發表文章

目前顯示的是 7月, 2017的文章

[LeetCode] 355. Design Twitter

Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods: postTweet(userId, tweetId) : Compose a new tweet. getNewsFeed(userId) : Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. follow(followerId, followeeId) : Follower follows a followee. unfollow(followerId, followeeId) : Follower unfollows a followee. Example: Twitter twitter = new Twitter(); // User 1 posts a new tweet (id = 5). twitter.postTweet(1, 5); // User 1's news feed should return a list with 1 tweet id -> [5]. twitter.getNewsFeed(1); // User 1 follows user 2. twitter.follow(1, 2); // User 2 posts a new tweet (id = 6). twitter.postTweet(2, 6); // User 1's news feed sho...

[LeetCode] 645. Set Mismatch

圖片
The set   S   originally contains numbers from 1 to   n . But unfortunately, due to the data error, one of the numbers in the set got duplicated to   another   number in the set, which results in repetition of one number and loss of another number. Given an array   nums   representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array. Example 1: Input: nums = [1,2,2,4] Output: [2,3] Note: The given array size will in the range [2, 10000]. The given array's numbers won't have any order. 思路: 用collections 做成字典, 掃字典一次