leetcode
leetcode-253. Meeting Rooms II
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],…] (si < ei), find the minimum number of conference rooms required.
For example,
Given [[0, 30],[5, 10],[15, 20]],
return 2.
原题地址
跟上一道题差不多的思路,我的代码如下:
在网上看到一个更好的思路,统计当前时间有多少会议开着。更加简洁明了。
leetcode-252. Meeting Rooms
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],…](si < ei), determine if a person could attend all meetings.
For example,
Given [[0, 30],[5, 10],[15, 20]],
return false.
原题地址
一道easy题,然而我zz一直没有反应过来。首先我们按照start time进行排序,要保证没有重叠的时间段,那么每一次的开始时间不能小于前一次的结束时间,即:一个会开完才能开下一个会。好吧,就这么简单。
leetcode-338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.
Example:
For num = 5 you should return [0,1,1,2,1,2].
原题地址
这是一道很简单的找规律题,这里想说下如何寻找规律。核心的思想是去掉一位,因为去除一位之后的数我们是求过的。去除的方法有很多,这里举几个例子:
- 去除最低位:右移即可,然后判断末位是否为1. f[i] = f[i >> 1] + i & 1;
- 去除最低位的1:f[i] = f[i & (i - 1)] + 1;
- 去除最高位的1:维护一个2^n的数,使2^n <= i < 2^(n + 1),那么f[i] = f[i - 2^n] + 1;
总之需要记住要跟去掉一位之后的数进行比较。
leetcode-340. Longest Substring with At Most K Distinct Characters
Given a string, find the length of the longest substring T that contains at most k distinct characters.
For example, Given s = “eceba” and k = 2,
T is “ece” which its length is 3.
Show Company Tags
Show Tags
Show Similar Problems
原题地址
利用hash table统计substring里的字符,类似的以char为key的题都可以用数组来做,会快一些。
leetcode-325. Maximum Size Subarray Sum Equals k
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn’t one, return 0 instead.
Example 1:
Given nums = [1, -1, 5, -2, 3], k = 3,
return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is the longest)
Example 2:
Given nums = [-2, -1, 2, 1], k = 1,
return 2. (because the subarray [-1, 2] sums to 1 and is the longest)
Follow Up:
Can you do it in O(n) time?
原题地址
利用hash table纪录nums[0]到nums[i]的和,寻找两个和的差等于k的最大距离。Time: O(n). Space: O(n).
后来发现可以一边存入hash table一边比较,因为只需要和之前的数据求差即可。速度可以更快,代码更简洁。
leetcode-246. Strobogrammatic Number
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Write a function to determine if a number is strobogrammatic. The number is represented as a string.
For example, the numbers “69”, “88”, and “818” are all strobogrammatic.
原题地址
首尾对比,只有五种匹配的方式:0-0, 1-1, 8-8, 6-9, 9-6。把这些存入hash table会方便许多。
leetcode-288. Unique Word Abbreviation
An abbreviation of a word follows the form
a) it –> it (no abbreviation)
b) d|o|g –> d1g
c) i|nternationalizatio|n –> i18n
d) l|ocalizatio|n –> l10n
Assume you have a dictionary and given a word, find whether its abbreviation is unique in the dictionary. A word’s abbreviation is unique if no other word from the dictionary has the same abbreviation.
Example:
Given dictionary = [ “deer”, “door”, “cake”, “card” ]
isUnique(“dear”) -> false
isUnique(“cart”) -> true
isUnique(“cane”) -> false
isUnique(“make”) -> true
原题地址
一道easy题,然而并不是很easy。思路很明确,用hashtable记下字典里的缩写,然后比较所给的单词。有一个细节是,如果所给的词在字典里有,并且这个词的缩写在字典的缩写里是唯一的,是要返回true的。直接的解决办法是在hashtable里记下缩写对应的每个单词或者他们的index,但是在网上看到了更好的办法。只需要存一个string,如果有不同单词对应一个缩写就把这个string设为空。代码如下:
leetcode-359. Logger Rate Limiter
Design a logger system that receive stream of messages along with its timestamps, each message should be printed if and only if it is not printed in the last 10 seconds.
Given a message and a timestamp (in seconds granularity), return true if the message should be printed in the given timestamp, otherwise returns false.
It is possible that several messages arrive roughly at the same time.
Example:
Logger logger = new Logger();
// logging string “foo” at timestamp 1
logger.shouldPrintMessage(1, “foo”); returns true;
// logging string “bar” at timestamp 2
logger.shouldPrintMessage(2,”bar”); returns true;
// logging string “foo” at timestamp 3
logger.shouldPrintMessage(3,”foo”); returns false;
// logging string “bar” at timestamp 8
logger.shouldPrintMessage(8,”bar”); returns false;
// logging string “foo” at timestamp 10
logger.shouldPrintMessage(10,”foo”); returns false;
// logging string “foo” at timestamp 11
logger.shouldPrintMessage(11,”foo”); returns true;
原题地址
Hash table的简单应用。
在网上看到一种更为简洁的写法,mark一下。
leetcode-163. Missing Ranges
Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges.
For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return [“2”, “4->49”, “51->74”, “76->99”].
原题地址
挺简单的一道题,不知道有什么可说的。
leetcode-277. Find the Celebrity
Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people know him/her but he/she does not know any of them.
Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: “Hi, A. Do you know B?” to get information of whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).
You are given a helper function bool knows(a, b) which tells you whether A knows B. Implement a function int findCelebrity(n), your function should minimize the number of calls to knows.
Note: There will be exactly one celebrity if he/she is in the party. Return the celebrity’s label if there is a celebrity in the party. If there is no celebrity, return -1.
原题地址
比较蠢的办法就是对每个人查看,如果他不认识其他人,并且其他人认识他,那他就是celebrity。Time: O(n2).
更好的办法是Two pass,第一遍先找到可能是celebrity的人,第二遍验证。Time: O(n).