摘要: Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order ofO(logn).If the target is not found in the array, return[-1, -1].For example,Given[5, 7, 7, 8, 8, 10]and target value 8,retu 阅读全文
posted @ 2013-04-29 16:54 caijinlong 阅读(126) 评论(0) 推荐(0) 编辑
摘要: Given a collection of integers that might contain duplicates,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,2], a solution is:[ [2], [1], [1,2,2], [2,2], [1,2], []]解法一:同样可以使用DFS,只... 阅读全文
posted @ 2013-04-29 14:31 caijinlong 阅读(164) 评论(0) 推荐(0) 编辑
摘要: 本文转自:http://blog.csdn.net/tuantuanls/article/details/8751495Given 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... 阅读全文
posted @ 2013-04-29 14:09 caijinlong 阅读(732) 评论(0) 推荐(0) 编辑
摘要: 本文转载:http://blog.csdn.net/zxzxy1988/article/details/8586289做了这道题,对backtracking的理解又加深了一点点。1 每个backtracking的题目,最好都有独立判断isValid的程序,这样架构清楚。同时,valid判断函数在这里可以稍微研究一下。只要当前要判断的位置上的数值和本行没有重复,本列没有重复,九宫格没有重复就可以。一旦重复立即返回,减少判断次数。2 backtracking的递归函数,怎么能没有返回值呢?!因为要判断递归的方案正确与否,所以这里的递归一定是有返回值的(除非是combination那种没有正确错误概 阅读全文
posted @ 2013-04-29 12:24 caijinlong 阅读(159) 评论(0) 推荐(0) 编辑
摘要: 1 int longestConsecutive(const vector<int> &num) { 2 unordered_map<int, int> hash; 3 for (int i=0; i<num.size(); ++i) 4 hash[num[i]] = 1; 5 6 int ans=0; 7 for (int i=0; i<num.size(); ++i) { 8 unordered_map<int, int>::iterator it = hash.find(num[i]+1);; 9 fo... 阅读全文
posted @ 2013-04-28 14:09 caijinlong 阅读(151) 评论(0) 推荐(0) 编辑
摘要: 1 class Solution { 2 public: 3 void solve(vector<vector<char>>& board) { 4 if (board.empty()) return; 5 int n = board.size(); 6 int m = board[0].size(); 7 // Top-most line 8 for (int j = 0; j < m; ++j) 9 if (board[0][j] == 'O')10 ... 阅读全文
posted @ 2013-04-28 13:18 caijinlong 阅读(123) 评论(0) 推荐(0) 编辑