摘要: Given a number represented as an array of digits, plus one to the number.class Solution {public: vector plusOne(vector &digits) { // Start typing your C/C++ solution below // DO NOT write int main() function vector ans; int c = 0, k = 0; for(int i = digits.size(... 阅读全文
posted @ 2013-07-10 22:54 一只会思考的猪 阅读(201) 评论(0) 推荐(0) 编辑
摘要: Implement int sqrt(int x).Compute and return the square root of x.思路:Sqrt(double)需要进行逼近。目前这种方法掌握不好class Solution { public: int sqrt(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function // Start typing your Java solution below... 阅读全文
posted @ 2013-07-10 22:37 一只会思考的猪 阅读(188) 评论(0) 推荐(0) 编辑
摘要: 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?class Solution {public: int climbStairs(int n) { // Start typing your C/C++ solution below // DO NOT write int main() f... 阅读全文
posted @ 2013-07-10 22:09 一只会思考的猪 阅读(137) 评论(0) 推荐(0) 编辑
摘要: Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c) Replace a character思路:普通的DP很好写,问题是路径压缩的DP压缩如果进行,空间如何重复利用 阅读全文
posted @ 2013-07-10 21:41 一只会思考的猪 阅读(153) 评论(0) 推荐(0) 编辑
摘要: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.思路:完全的in-place,利用了第一次出现0位置的第i行和第j列存储将来可能出现0的行的下标和列的下标class Solution {public: void setZeroes(vector > &matrix) { // Start typing your C/C++ solution below // DO NOT write int main() function ... 阅读全文
posted @ 2013-07-10 21:18 一只会思考的猪 阅读(187) 评论(0) 推荐(0) 编辑
摘要: Write an efficient algorithm that searches for a value in an m x n matrix. 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, .. 阅读全文
posted @ 2013-07-10 04:36 一只会思考的猪 阅读(132) 评论(0) 推荐(0) 编辑
摘要: 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 t 阅读全文
posted @ 2013-07-10 04:28 一只会思考的猪 阅读(227) 评论(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],]class Solution {public: void dfs(vector> &ans, vector path,int n, int k, int ok_n){ if (ok_n == k){ ans.... 阅读全文
posted @ 2013-07-10 03:59 一只会思考的猪 阅读(177) 评论(0) 推荐(0) 编辑