2013年10月1日

Combinations

摘要: 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],]Solution:采用位运算来做,但是这样时间复杂度很高O(n* 2^ n)。 1 public class Solution { 2 public ArrayList> combine(int n, int k) { 3 /... 阅读全文

posted @ 2013-10-01 14:42 Step-BY-Step 阅读(149) 评论(0) 推荐(0) 编辑

Search a 2D Matrix

摘要: 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-10-01 14:25 Step-BY-Step 阅读(161) 评论(0) 推荐(0) 编辑

Set Matrix Zeroes

摘要: Given amxnmatrix, 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 de 阅读全文

posted @ 2013-10-01 13:44 Step-BY-Step 阅读(204) 评论(0) 推荐(0) 编辑

Edit Distance

摘要: Given two wordsword1andword2, find the minimum number of steps required to convertword1toword2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) Insert a characterb) Delete a characterc) Replace a characterSolution:第一遍做的时候没弄懂应该怎么做,只是有个感觉应该是DP。对于一个位置,它有 阅读全文

posted @ 2013-10-01 12:49 Step-BY-Step 阅读(164) 评论(0) 推荐(0) 编辑

Sort Colors

摘要: Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, wh... 阅读全文

posted @ 2013-10-01 11:40 Step-BY-Step 阅读(134) 评论(0) 推荐(0) 编辑

导航