Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
Your algorithm should run in O(n) complexity.
Example:
**Input:** [100, 4, 200, 1, 3, 2]
**Output:** 4
**Explanation:** The longest consecutive elements sequence is `[1, 2, 3, 4]`. Therefore its length is 4.
deflongestConsecutive(self, nums: List[int]) -> int: num_set = collections.Counter(nums) res = 0 for n in nums: if num_set[n] == 0: continue max_len = 1 num_set[n] = 0 i = n + 1 while i in num_set: num_set[i] = 0 max_len += 1 i += 1 i = n - 1 while i in num_set: num_set[i] = 0 max_len += 1 i -= 1 res = max(res, max_len) return res
存储系统可以选择SQL或者No SQL。No SQL就会更加scalable.这里可以讨论它们之间的pros和cons。 传输协议(client-server)可用Java中的Socket和ServerSocket对象,它们建立一个TCP连接,用IO Stream传输。 服务器端用多个服务器避免single point of failure。server端的memory会存一些用户状态等数据(当然它也会被持久化),这表示它需要 replicate一些数据减少不同机器之间的lookup时间。
Conversation: int id; List<User> users; List<Message> messages; voidaddMessage(Message msg); PrivateChat extends Conversation: PrivateChat(User user, User user2);
GroupChat extends Conversation: voidaddUser(User u); voidremoveUser(User u); Message: int id; Date timestamp; User user;//I would like to know who sent this msg String content;
FriendRequest: User from; User to; Date timestamp; RequestStatus Status;
public String translateThree(int number){ StringBuilder result = new StringBuilder(); if(number > 99) { result.append(" "+lows[number / 100]); result.append(" Hundred"); number = number % 100; }
if(number > 19) { result.append(" "); result.append(tens[number/10]); number = number % 10; }
// Remainder is under 20 result.append(" "+lows[number]); return" "+result.toString().trim(); }
public String reverse(String s){ return (new StringBuilder()).append(s).reverse().toString(); }