摘要: Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array.---public class Solution { public boolean search(int[] A, int target) { return helper(A... 阅读全文
posted @ 2013-09-16 10:20 LEDYC 阅读(154) 评论(0) 推荐(0)
摘要: Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space.---constant space, --> swap!桶排序,只不过不许用额外空间。每次当A[i]!= i的时候,将A[i]与A[A[i]]交换,也就是把A[i]放到它应该在的位置A[A[i]]直到无法交换位置。-- 阅读全文
posted @ 2013-09-15 10:33 LEDYC 阅读(137) 评论(0) 推荐(0)
摘要: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number inCmay only be usedoncein the combination.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, � ,ak) must 阅读全文
posted @ 2013-09-15 10:10 LEDYC 阅读(179) 评论(0) 推荐(0)
摘要: Follow up for "Remove Duplicates":What if duplicates are allowed at mosttwice?For example,Given sorted array A =[1,1,1,2,2,3],Your function should return length =5, and A is now[1,1,2,2,3].---public class Solution { public int removeDuplicates(int[] A) { if(A.length <= 2) return ... 阅读全文
posted @ 2013-09-15 08:12 LEDYC 阅读(164) 评论(0) 推荐(0)
摘要: 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=[ [&q 阅读全文
posted @ 2013-09-15 07:57 LEDYC 阅读(186) 评论(0) 推荐(0)
摘要: Given a set of distinct integers,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,IfS=[1,2,3], a solution is:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]---Compare to leet 77, combin... 阅读全文
posted @ 2013-09-15 07:44 LEDYC 阅读(142) 评论(0) 推荐(0)
摘要: Given two integersnandk, return all possible combinations ofknumbers 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],]---YC 10/20 versionpublic class Solution { public ArrayList> combine(int n, int k) { ArrayList> rst = new Ar... 阅读全文
posted @ 2013-09-15 07:30 LEDYC 阅读(155) 评论(0) 推荐(0)
摘要: 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 阅读全文
posted @ 2013-09-15 07:22 LEDYC 阅读(197) 评论(0) 推荐(0)
摘要: Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previous row.For example,Consider the following matrix:[ [1, 3, ... 阅读全文
posted @ 2013-09-12 00:19 LEDYC 阅读(166) 评论(0) 推荐(0)
摘要: Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.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 so 阅读全文
posted @ 2013-09-12 00:13 LEDYC 阅读(126) 评论(0) 推荐(0)