码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 锁掉的题目清单

时间:2016-02-10 12:05:05      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

也刷leetcode, 先把锁掉的题目留备份好了:

156 Binary Tree Upside Down 

 [1]

Problem:

Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root.

For example:
Given a binary tree {1,2,3,4,5},

    1
   /   2   3
 / 4   5

 

return the root of the binary tree [4,5,2,#,#,3,1].

   4
  /  5   2
    /    3   1  

 

confused what "{1,#,2,3}" means? 

 

157 Read N Characters Given Read4 

 [2]

Question:
The API: int read4(char *buf) reads 4 characters at a time from a file.
The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file.
By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.

Note:
The read function will only be called once for each test case.


Hint:
Consider which one is smaller, read4(buf) or n - num.

 

158 Read N Characters Given Read4 II - Call multiple times 

 [3]

Question:
The API: int read4(char *buf) reads 4 characters at a time from a file.
The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file.
By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.

Note:
The read function may be called multiple times.

Hint:
Use a global queue to store the left characters after read().

 

159 Longest Substring with At Most Two Distinct Characters 

[4]

Given a string S, find the length of the longest substring T that contains at most two distinct characters.
For example,
Given S = “eceba”,
T is “ece” which its length is 3.

找到最多含有两个不同字符的子串的最长长度。例如:eoeabc,最长的是eoe为3,其他都为2. [4]

思路:

用p1,p2表示两种字符串的最后一个出现的下标位置。初始p1为0. p2为-1.start初始化为0,表示两种字符串的开头。

只要遍历一次string就可以得到结果了。

首先我们要确定p2的值,那么i要一直到不等于s[p1]的值为止,那么位置就是p2了。

然后继续往后如果来一个字符等于之前两种的其中一种,那么就要更新最后一次出现的下标。根据是谁就更新谁。

如果是新的字符了,那么就要更新start了,start肯定就是小的那个p1+1,因为p1是一种字符的最后一个位置,他的下一位肯定就是另一个字符了。

每次计算大的结束点,p1或者p2,与start中间的个数就是我们要的长度了。最后返回最长的就是了。

因为存在只有一种字符的情况,所以要判断如果p2最后还是-1,那么就返回整个串的长度就是了。

 

161 One Edit Distance 

  [5]

Description:
Given two strings S and T, determine if they are both one edit distance apart.

Hints:

There are three 1-edit distance operations shown as below:
1. Modify (shift = 0):

abcde
abXde
2. Append (shift = 1):
abcde
abcdeX
3. Insert (shift = 1):
abcde
abcdeX
We use shift to show the difference of the length of two strings.
 
 [6]

Missing Ranges

 Total Accepted: 510 Total Submissions: 2300
 

Given a sorted integer array where the range of elements are [lowerupper] 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"].

 

[分析]

O(N) 扫一遍即可, 跟新LOWER, 

[注意]

两层循环, 共用一个i时, 内层循环要注意判断边界条件.

 

 [7]
Description:
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
[8]
Description:
Design and implement a TwoSum class. It should support the following operations: add and find.

add - Add the number to an internal data structure.
find - Find if there exists any pair of numbers which sum is equal to the value.

For example,

add(1); add(3); add(5);
find(4) -> true
find(7) -> false
 
 

186 Reverse Words in a String II 

 [9]

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.

The input string does not contain leading or trailing spaces and the words are always separated by a single space.

For example,
Given s = "the sky is blue",
return "blue is sky the".

Could you do it in-place without allocating extra space? 

 

243 Shortest Word Distance 

 [10]

 

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.

 

For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

 

Given word1 = “coding”word2 = “practice”, return 3.
Given word1 = "makes"word2 = "coding", return 1.

 

Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

  

244 Shortest Word Distance II 

  [11]

 Question:
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?

Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list.

For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Given word1 = “coding”, word2 = “practice”, return 3.
Given word1 = "makes", word2 = "coding", return 1.

Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.


Hints:
Use a hash table mapping word to index list.

  

245 Shortest Word Distance III 

  [12]

This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2.
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
word1 and word2 may be the same and they represent two individual words in the list.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Given word1 = “makes”word2 = “coding”, return 1.
Given word1 = "makes"word2 = "makes", return 3.
Note:
You may assume word1 and word2 are both in the list.

  

246 Strobogrammatic Number 

 [13]

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.
 
 [14]
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Find all strobogrammatic numbers that are of length = n.
For example,
Given n = 2, return ["11","69","88","96"].
Hint:
  1. Try to use recursion and notice that it should recurse with n - 2 instead of n - 1.

      

 [15]

 

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

 

Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high.

 

For example,
Given low = "50", high = "100", return 3. Because 69, 88, and 96 are three strobogrammatic numbers.

 

Note:
Because the range might be a large number, the low and high numbers are represented as string.

  

249 Group Shifted Strings 

  [16]

 Given a string, we can “shift” each of its letter to its successive letter, for example: “abc” -> “bcd”. We can keep “shifting” which forms the sequence:

“abc” -> “bcd” -> … -> “xyz”
Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence.

For example, given: [“abc”, “bcd”, “acef”, “xyz”, “az”, “ba”, “a”, “z”],
Return:

[
[“abc”,”bcd”,”xyz”],
[“az”,”ba”],
[“acef”],
[“a”,”z”]
]
Note: For the return value, each inner list’s elements must follow the lexicographic order.

  

250 Count Univalue Subtrees 

  [17]

Given a binary tree, count the number of uni-value subtrees.
A Uni-value subtree means all nodes of the subtree have the same value.
For example:
Given binary tree,
              5
             /             1   5
           / \             5   5   5
 
return 4.

  

251 Flatten 2D Vector 

  [18]

Implement an iterator to flatten a 2d vector.

For example,
Given 2d vector =

[
  [1,2],
  [3],
  [4,5,6]
]

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be:[1,2,3,4,5,6].

Hint:

  1. How many variables do you need to keep track?
  2. Two variables is all you need. Try with x and y.
  3. Beware of empty rows. It could be the first few rows.
  4. To write correct code, think about the invariant to maintain. What is it?
  5. The invariant is x and y must always point to a valid point in the 2d vector. Should you maintain your invariant ahead of time or right when you need it?
  6. Not sure? Think about how you would implement hasNext(). Which is more complex?
  7. Common logic in two different places should be refactored into a common method.

  

252 Meeting Rooms 

  [19] 

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.
 
Show Company Tags
Show Tags
Show Similar Problems

 

  [20] 

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.  

 

254 Factor Combinations 

  [21] 

Problem:

Numbers can be regarded as product of its factors. For example, 

8 = 2 x 2 x 2;
  = 2 x 4.

Write a function that takes an integer n and return all possible combinations of its factors. 

Note: 

  1. Each combination‘s factors must be sorted ascending, for example: The factors of 2 and 6 is [2, 6], not [6, 2]
  2. You may assume that n is always positive. 
  3. Factors should be greater than 1 and less than n.

 

Examples: 
input: 1
output: 

[]

input: 37
output: 

[]

input: 12
output:

[
  [2, 6],
  [2, 2, 3],
  [3, 4]
]

input: 32
output:

[
  [2, 16],
  [2, 2, 8],
  [2, 2, 2, 4],
  [2, 2, 2, 2, 2],
  [2, 4, 4],
  [4, 8]
]

255 Verify Preorder Sequence in Binary Search Tree 

 [22]

Given an array of numbers, 
verify whether it is the correct preorder traversal sequence of a binary search tree.
You may assume each number in the sequence is unique.
Follow up:
Could you do it using only constant space complexity?
 
 
256 Paint House 

 [23]

There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.
The cost of painting each house with a certain color is represented by a n x 3 cost matrix. For example, costs[0][0] is the cost of painting house 0 with color red;costs[1][2] is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.
Note:
All costs are positive integers.

  

259 3Sum Smaller 

  [24]

Total Accepted: 1706 Total Submissions: 5006 Difficulty: Medium

Given an array of n integers nums and a target, find the number of index triplets i, j, kwith 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.

For example, given nums = [-2, 0, 1, 3], and target = 2.

Return 2. Because there are two triplets which sums are less than 2:

 

 

Follow up:
Could you solve it in O(n2) runtime?

  

261 Graph Valid Tree 

 [25]

Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.

For example:

Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], return true.

Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]], return false.

Hint:

  1. Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], what should your return? Is this case a valid tree? Show More Hint 

Note: you can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.

  

265 Paint House II 

 

 [26] 

Problem:

 

There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.

 

The cost of painting each house with a certain color is represented by a n x k cost matrix. For example, costs[0][0] is the cost of painting house 0 with color 0; costs[1][2] is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.

 

Note:
All costs are positive integers.

 

Follow up:
Could you solve it in O(nk) runtime?

  

266 Palindrome Permutation 

  [27]

Given a string, determine if a permutation of the string could form a palindrome.

For example,
“code” -> False, “aab” -> True, “carerac” -> True.

Hint:

Consider the palindromes of odd vs even length. What difference do you notice?
Count the frequency of each character.
If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times?

// 利用set,如果set没有就存入,有的话就取出。这样最后剩下0个或者1个的时候就是回文数

 

267 Palindrome Permutation II 

 [28]

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form.
For example:
Given s = "aabb", return ["abba", "baab"].
Given s = "abc", return [].
 
269 Alien Dictionary 

 [29]

There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the dictionary, wherewords are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.

For example,
Given the following words in dictionary,

 

 

The correct order is: "wertf".

Note:

    1. You may assume all letters are in lowercase.
    2. If the order is invalid, return an empty string.
    3. There may be multiple valid order of letters, return any one of them is fine.

 

270 Closest Binary Search Tree Value 

 [30]

Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.

Note:

  • Given target value is a floating point.
  • You are guaranteed to have only one unique value in the BST that is closest to the target.

 

271 Encode and Decode Strings 

 [31]

Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings.
Machine 1 (sender) has the function:
string encode(vector<string> strs) {
  // ... your code
  return encoded_string;
}

Machine 2 (receiver) has the function:

vector<string> decode(string s) {
  //... your code
  return strs;
}
 
So Machine 1 does:
string encoded_string = encode(strs);
 
and Machine 2 does:
vector<string> strs2 = decode(encoded_string);
 
strs2 in Machine 2 should be the same as strs in Machine 1.
Implement the encode and decode methods.
Note:
  • The string may contain any possible characters out of 256 valid ascii characters. Your algorithm should be generalized enough to work on any possible characters.
  • Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
  • Do not rely on any library method such as eval or serialize methods. You should implement your own encode/decode algorithm.

 

272 Closest Binary Search Tree Value II 

 [32]

Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.

Note:

  • Given target value is a floating point.
  • You may assume k is always valid, that is: k ≤ total nodes.
  • You are guaranteed to have only one unique set of k values in the BST that are closest to the target.

Follow up:
Assume that the BST is balanced, could you solve it in less than O(n) runtime (where n = total nodes)?

Hint:

    1. Consider implement these two helper functions:Try to assume that each node has a parent pointer, it makes the problem much easier.
      1. getPredecessor(N), which returns the next smaller node to N.
      2. getSuccessor(N), which returns the next larger node to N.
    2. Without parent pointer we just need to keep track of the path from the root to the current node using a stack.
    3. You would need two stacks to track the path in finding predecessor and successor node separately.

 

276 Paint Fence 

 [33]

Problem:

There is a fence with n posts, each post can be painted with one of the k colors.

You have to paint all the posts such that no more than two adjacent fence posts have the same color. 

Return the total number of ways you can paint the fence. 

Note:
n and k are non-negative integers.

 

277 Find the Celebrity 

 [34]

Suppose you are at a party with n people (labeled from 0 ton - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the othern - 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 functionint findCelebrity(n), your function should minimize the number of calls toknows.

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.

 

280 Wiggle Sort 

 [35]

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]....

For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4].

 

 

281 Zigzag Iterator 

  [36] 

Given two 1d vectors, implement an iterator to return their elements alternately.
For example, given two 1d vectors:
v1 = [1, 2]
v2 = [3, 4, 5, 6]
 
By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1, 3, 2, 4, 5, 6].
Follow up: What if you are given k 1d vectors? How well can your code be extended to such cases?
Clarification for the follow up question - Update (2015-09-18):
The "Zigzag" order is not clearly defined and is ambiguous for k > 2 cases. If "Zigzag" does not look right to you, replace "Zigzag" with "Cyclic". For example, given the following input:
[1,2,3]
[4,5,6,7]
[8,9]

It should return [1,4,8,2,5,9,3,6,7].

 

285 Inorder Successor in BST 

 [37]

Given a binary search tree and a node in it, find the in-order successor of that node in the BST.

Note: If the given node has no in-order successor in the tree, return null.

 

286 Walls and Gates 

 [38]

You are given a m x n 2D grid initialized with these three possible values.
  1. -1 - A wall or an obstacle.
  2. 0 - A gate.
  3. INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than2147483647.
Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.
For example, given the 2D grid:
INF  -1  0  INF
INF INF INF  -1
INF  -1 INF  -1
  0  -1 INF INF
 
After running your function, the 2D grid should be:
  3  -1   0   1
  2   2   1  -1
  1  -1   2  -1
  0  -1   3   4

 

288 Unique Word Abbreviation 

 [39]

An abbreviation of a word follows the form <first letter><number><last letter>. Below are some examples of word abbreviations:
a) it                      --> it    (no abbreviation)

     1
b) d|o|g                   --> d1g

              1    1  1
     1---5----0----5--8
c) i|nternationalizatio|n  --> i18n

              1
     1---5----0
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


291 Word Pattern II 
 [40]

Given a pattern and a string str, find if str follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in str.

Examples:

  1. pattern = "abab", str = "redblueredblue" should return true.
  2. pattern = "aaaa", str = "asdasdasdasd" should return true.
  3. pattern = "aabb", str = "xyzabcxzyabc" should return false.

Notes:
You may assume both pattern and str contains only lowercase letters.

  
293 Flip Game 
 [41]
You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip twoconsecutive "++" into "--". The game ends when a person can no longer make a move and therefore the other person will be the winner.
Write a function to compute all possible states of the string after one valid move.
For example, given s = "++++", after one move, it may become one of the following states:
[
  "--++",
  "+--+",
  "++--"
] 
If there is no valid move, return an empty list [].
 
294 Flip Game II 
 [42]

You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive "++" into "--". The game ends when a person can no longer make a move and therefore the other person will be the winner.

Write a function to determine if the starting player can guarantee a win.

For example, given s = "++++", return true. The starting player can guarantee a win by flipping the middle "++" to become "+--+".

Follow up:
Derive your algorithm’s runtime complexity.

 

296 Best Meeting Point 
 [43]
A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.
For example, given three people living at (0,0)(0,4), and (2,2):
1 - 0 - 0 - 0 - 1
|   |   |   |   |
0 - 0 - 0 - 0 - 0
|   |   |   |   |
0 - 0 - 1 - 0 - 0
The point (0,2) is an ideal meeting point, as the total travel distance of 2+2+2=6 is minimal. So return 6.
 
298 Binary Tree Longest Consecutive Sequence 
 [44]

Given a binary tree, find the length of the longest consecutive sequence path.

 

The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from parent to child (cannot be the reverse).

For example,

   1
         3
    /    2   4
                 5

Longest consecutive sequence path is 3-4-5, so return 3.

   2
         3
    / 
   2    
  / 
 1

Longest consecutive sequence path is 2-3,not3-2-1, so return 2.

 
302 Smallest Rectangle Enclosing Black Pixels 
 [45]

An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.

For example, given the following image:

[
  "0010",
  "0110",
  "0100"
]

and x = 0y = 2, 

Return 6.

 
305 Number of Islands II 
 [46]
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand operation. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

308 Range Sum Query 2D - Mutable 

  [47]

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

技术分享
The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.

Example:

Given matrix = [
  [3, 0, 1, 4, 2],
  [5, 6, 3, 2, 1],
  [1, 2, 0, 1, 5],
  [4, 1, 0, 1, 7],
  [1, 0, 3, 0, 5]
]

sumRegion(2, 1, 4, 3) -> 8
update(3, 2, 2)
sumRegion(2, 1, 4, 3) -> 10

 

Note:

  1. The matrix is only modifiable by the update function.
  2. You may assume the number of calls to update and sumRegion function is distributed evenly.
  3. You may assume that row1 ≤ row2 and col1 ≤ col2.

 

311 Sparse Matrix Multiplication 

 [48]

Given two sparse matrices A and B, return the result of AB.

You may assume that A‘s column number is equal to B‘s row number.

Example:

A = [
  [ 1, 0, 0],
  [-1, 0, 3]
]

B = [
  [ 7, 0, 0 ],
  [ 0, 0, 0 ],
  [ 0, 0, 1 ]
]


     |  1 0 0 |   | 7 0 0 |   |  7 0 0 |
AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
                  | 0 0 1 |

 

314 Binary Tree Vertical Order Traversal 

 [49]

Given a binary tree, return the vertical order traversal of its nodes‘ values. (ie, from top to bottom, column by column).

If two nodes are in the same row and column, the order should be from left to right.

Examples:
Given binary tree [3,9,20,null,null,15,7],

    3
   /   9  20
    /     15   7

 

return its vertical order traversal as:

[
  [9],
  [3,15],
  [20],
  [7]
]

 

Given binary tree [3,9,20,4,5,2,7],

    _3_
   /     9    20
 / \   / 4   5 2   7

 

return its vertical order traversal as:

[
  [4],
  [9],
  [3,5,2],
  [20],
  [7]
]

 

317 Shortest Distance from All Buildings 

 [50]

You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You are given a 2D grid of values 0, 1 or 2, where:

Each 0 marks an empty land which you can pass by freely.
Each 1 marks a building which you cannot pass through.
Each 2 marks an obstacle which you cannot pass through.
The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x – p1.x| + |p2.y – p1.y|.

For example, given three buildings at (0,0), (0,4), (2,2), and an obstacle at (0,2):

1 – 0 – 2 – 0 – 1
|    |     |     |    |
0 – 0 – 0 – 0 – 0
|     |     |     |    |
0 – 0 – 1 – 0 – 0
The point (1,2) is an ideal empty land to build a house, as the total travel distance of 3+3+1=7 is minimal. So return 7.

Note:
There will be at least one building. If it is not possible to build such house according to the above rules, return -1.

 

320 Generalized Abbreviation 

  [51]

Write a function to generate the generalized abbreviations of a word.
Example:
Given word = "word", return the following list (order does not matter):
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]

 

323 Number of Connected Components in an Undirected Graph 

 [52] [53]

Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph.

Example 1:
     0          3
     |          |
     1 --- 2    4
Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], return 2.

Example 2:
     0           4
     |           |
     1 --- 2 --- 3
Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [3, 4]], return 1.

Note:
You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.
 
 
 [54]

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?

 

统计完毕 合计53题。

 

Refernce:

[1]http://www.cnblogs.com/airwindow/p/4809040.html

[2]http://www.tangjikai.com/algorithms/leetcode-157-158-read-n-characters-given-read4-i-ii-call-multiply-times

[3]http://yuanhsh.iteye.com/blog/2188917

[4]http://www.cnblogs.com/higerzhang/p/4185649.html

[5]http://www.tangjikai.com/algorithms/leetcode-161-one-edit-distance

[6]http://blog.csdn.net/xudli/article/details/42290511

[7]http://www.tangjikai.com/algorithms/leetcode-2-two-sum-ii-input-array-is-sorted

[8]http://ask.julyedu.com/question/107

[9]http://ask.julyedu.com/question/132

[10]http://nb4799.neu.edu/wordpress/?p=1025

[11]http://www.tangjikai.com/algorithms/leetcode-244-shortest-word-distance-ii

[12]http://leetcode0.blogspot.com/2015/12/245-shortest-word-distance-iii-my.html

[13]http://leetcode0.blogspot.com/2015/12/246-strobogrammatic-number-my.html

[14]http://leetcode0.blogspot.com/2015/12/247-strobogrammatic-number-ii.html

[15]http://happycoding2010.blogspot.com/2015/11/leetcode-248-strobogrammatic-number-iii.html

[16]http://codingmelon.com/2015/12/25/group-shifted-strings-leetcode-249/

[17]http://happycoding2010.blogspot.com/2015/11/leetcode-250-count-univalue-subtrees.html

[18]http://blog.csdn.net/pointbreak1/article/details/48823513

[19]http://leetcode0.blogspot.com/2015/12/252-meeting-rooms.html

[20]http://liuy966.blogspot.com/2015/09/leetcode-253-meeting-rooms-ii.html

[21]http://www.cnblogs.com/airwindow/p/4822572.html

[22]http://leetcode0.blogspot.com/2015/12/255-verify-preorder-sequence-in-binary.html

[23]http://happycoding2010.blogspot.com/2015/11/leetcode-256-paint-house.html

[24]http://nb4799.neu.edu/wordpress/?p=844

[25]http://www.cnblogs.com/yrbbest/p/5018217.html

[26]http://www.cnblogs.com/airwindow/p/4804011.html

[27]https://leetcodesite.wordpress.com/2016/02/08/266-palindrome-permutation/

[28]http://happycoding2010.blogspot.com/2015/11/leetcode-267-palindrome-permutation-ii.html

[29]http://nb4799.neu.edu/wordpress/?p=923

[30]http://blog.csdn.net/xudli/article/details/48749493

[31]http://happycoding2010.blogspot.com/2015/11/leetcode-271-encode-and-decode-strings.html

[32]http://blog.csdn.net/xudli/article/details/48752907

[33]http://www.cnblogs.com/airwindow/p/4796688.html

[34]http://blog.csdn.net/xudli/article/details/48749295

[35]http://nb4799.neu.edu/wordpress/?p=841

[36]http://happycoding2010.blogspot.com/2015/11/leetcode-281-zigzag-iterator.html

[37]http://www.arbrash1989.com/2016/01/14/285-Inorder-Successor-in-BST/

[38]http://happycoding2010.blogspot.com/2015/11/leetcode-286-walls-and-gates.html

[39]http://happycoding2010.blogspot.com/2015/11/leetcode-288-unique-word-abbreviation.html

[40]http://yefangliang.com/2015/10/12/leetcode-291-word-pattern-ii/

[41]http://happycoding2010.blogspot.com/2015/11/leetcode-293-flip-game.html

[42]http://yefangliang.com/2015/10/16/leetcode-294-flip-game-ii/

[43]http://happycoding2010.blogspot.com/2015/11/leetcode-296-best-meeting-point.html

[44]http://www.cnblogs.com/yrbbest/p/5047038.html

[45]http://www.cnblogs.com/yrbbest/p/5050022.html

[46]http://codingmelon.com/2015/12/13/number-of-islands-ii-leetcode-305/

[47]http://www.cnblogs.com/yrbbest/p/5058571.html

[48]http://www.cnblogs.com/yrbbest/p/5060667.html

[49]http://www.cnblogs.com/yrbbest/p/5065457.html

[50]http://codingmelon.com/2015/12/13/shortest-distance-from-all-buildings-leetcode-317/

[51]http://leetcode0.blogspot.com/2015/12/320-generalized-abbreviation.html

[52]http://www.cnblogs.com/EdwardLiu/p/5088502.html

[53]https://asanchina.wordpress.com/2015/12/29/323-number-of-connected-components-in-an-undirected-graph/

[54]http://blog.csdn.net/bearkino/article/details/50480213

[**]https://github.com/yubinbai/leetcode

leetcode 锁掉的题目清单

标签:

原文地址:http://www.cnblogs.com/robinalee/p/5185497.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!