版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/10/06/2013-10-06-CODE 63 Sqrt(x)/
访问原文「CODE 63. Sqrt(x)」
Implement int sqrt(int x)
.
Compute and return the square root of x.
|
|
戒急用忍
版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/10/06/2013-10-06-CODE 63 Sqrt(x)/
访问原文「CODE 63. Sqrt(x)」
Implement int sqrt(int x)
.
Compute and return the square root of x.
|
|
版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/10/06/2013-10-06-CODE 62 Climbing Stairs/
访问原文「CODE 62. Climbing Stairs」
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
|
|
版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/10/06/2013-10-06-CODE 61 Simplify Path/
访问原文「CODE 61. Simplify Path」
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
click to show corner cases.
Corner Cases:
"/../"
?"/"
.'/'
together,"/home//foo/"
."/home/foo"
.
|
|
版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/10/05/2013-10-05-CODE 60 Set Matrix Zeroes/
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.
Follow up:Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m+n) space, but still not the best solution.
Could you devise a constant space solution?
|
|
版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/10/04/2013-10-04-CODE 59 Search a 2D Matrix/
Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Giventarget=3
, returntrue
.
|
|
版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/10/04/2013-10-04-CODE 58 Sort Colors/
访问原文「CODE 58. Sort Colors」
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:
You are not suppose to use the library’s sort function for this problem.
click to show follow up.
Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0’s, 1’s, and 2’s, then overwrite array with total number of 0’s, then 1’s and followed by 2’s.
Could you come up with an one-pass algorithm using only constant space?
WORST CODE:
|
|
版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/10/04/2013-10-04-CODE 57 Minimum Window Substring/
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,
S="ADOBECODEBANC"
T="ABC"
Minimum window is"BANC"
.
Note:
If there is no such window in S that covers all characters in T, return the emtpy string""
.
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
WORST CODE:
|
|
版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/10/03/2013-10-03-CODE 56 Combinations/
访问原文「CODE 56. Combinations」
Given two integers n and k, return all possible combinations ofk numbers out of 1 … n.For example,
Ifn= 4 andk= 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
1234567891011121314151617181920212223242526272829303132333435363738
public ArrayList<ArrayList<Integer>> combine(int n, int k) { // Start typing your Java solution below // DO NOT write main() function if (n < k) { return null; } ArrayList<Integer> sList = new ArrayList<Integer>(); for (int i = 1; i <= n; i++) { sList.add(i); } ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); for (int i = 1; i <= n; i++) { ArrayList<Integer> element = new ArrayList<Integer>(); element.add(i); result.add(element); } int start = 0; int end = result.size(); for (int num = 2; num <= k; num++) { ArrayList<ArrayList<Integer>> tmp = new ArrayList<ArrayList<Integer>>(); for (int i = start; i < end; i++) { ArrayList<Integer> oldElement = result.get(i); for (int j = sList .indexOf(oldElement.get(oldElement.size() - 1)) + 2; j <= n; j++) { ArrayList<Integer> element = new ArrayList<Integer>(); element.addAll(oldElement); element.add(j); tmp.add(element); } } result.clear(); result.addAll(tmp); start = 0; end = result.size(); } return result;}
版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/10/01/2013-10-01-CODE 55 Subsets/
访问原文「CODE 55. Subsets」
Given a set of distinct integers, S, return all possible subsets.Note:
For example,
IfS=[1,2,3]
, a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
123456789101112131415161718192021222324252627282930313233343536373839
public ArrayList<ArrayList<Integer>> subsets(int[] S) { // Start typing your Java solution below // DO NOT write main() function if (null == S || S.length <= 0) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); result.add(new ArrayList<Integer>()); return result; } Arrays.sort(S); ArrayList<Integer> sList = new ArrayList<Integer>(); for (int i = 0; i < S.length; i++) { sList.add(S[i]); } ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); for (int i = 0; i < S.length; i++) { ArrayList<Integer> element = new ArrayList<Integer>(); element.add(S[i]); result.add(element); } int start = 0; int end = result.size(); for (int num = 2; num <= S.length; num++) { for (int i = start; i < end; i++) { ArrayList<Integer> oldElement = result.get(i); for (int j = sList .indexOf(oldElement.get(oldElement.size() - 1)) + 1; j < S.length; j++) { ArrayList<Integer> element = new ArrayList<Integer>(); element.addAll(oldElement); element.add(S[j]); result.add(element); } } start = end; end = result.size(); } result.add(new ArrayList<Integer>()); return result;}
版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/10/01/2013-10-01-CODE 54 Word Search/
访问原文「CODE 54. Word Search」
Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Givenboard=
[
[“ABCE”],
[“SFCS”],
[“ADEE”]
]
word = "ABCCED"
, -> returnstrue
,
word = "SEE"
, -> returnstrue
,
word = "ABCB"
, -> returnsfalse
.
|
|