摘要: 1 /** 2 * Definition for undirected graph. 3 * struct UndirectedGraphNode { 4 * int label; 5 * vector neighbors; 6 * UndirectedGraphNode(int x) : label(x) {}; 7 * }; 8 */ 9 class Solution {10 public:11 12 UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node, unordered_ma... 阅读全文
posted @ 2013-10-28 01:21 tanghulu321 阅读(182) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: int canCompleteCircuit(vector &gas, vector &cost) { // Note: The Solution object is instantiated only once and is reused by each test case. if (gas.size() != cost.size()) return -1; vector diff; int total = 0; for (int i = 0; i < ... 阅读全文
posted @ 2013-10-27 12:36 tanghulu321 阅读(227) 评论(0) 推荐(0) 编辑
摘要: 1 /** 2 * Definition for singly-linked list with a random pointer. 3 * struct RandomListNode { 4 * int label; 5 * RandomListNode *next, *random; 6 * RandomListNode(int x) : label(x), next(NULL), random(NULL) {} 7 * }; 8 */ 9 class Solution {10 public:11 RandomListNode *copyRa... 阅读全文
posted @ 2013-10-27 06:27 tanghulu321 阅读(169) 评论(0) 推荐(0) 编辑
摘要: 1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 // Note: The Solution object is instantiated only once and is reused by each test case. 5 if (n == 0) return 0; 6 7 int bit[32]; 8 int result = 0; 9 10 for (int i = 0; i ... 阅读全文
posted @ 2013-10-27 03:44 tanghulu321 阅读(144) 评论(0) 推荐(0) 编辑
摘要: DP+剪枝class Solution {public: bool dfs(string s, unordered_set &dict, unordered_set &unmatch, int maxLen, vector &result,string path){ if (s.size() == 0){ path.pop_back(); result.push_back(path); return true; } bool flag = f... 阅读全文
posted @ 2013-10-27 03:12 tanghulu321 阅读(168) 评论(0) 推荐(0) 编辑
摘要: DP + 剪枝 1 class Solution{ 2 public: 3 bool wordBreakHelper(string s, unordered_set &dict,unordered_set &unmatch, int maxLength) { 4 if(s.length() == 0) return true; 5 6 for(int i = 1; i &dict) {20 21 if(s.size() == 0) return true;22 unordered... 阅读全文
posted @ 2013-10-27 02:02 tanghulu321 阅读(160) 评论(0) 推荐(0) 编辑
摘要: P: The problems that can be solved in polynomial time.NP: The problems whose solutions can be verified in polynomial time.NP-complete: The problems in the set of NP problems and also in the set of NP-hard problems.Although any given solution to such a problem can be verified qu... 阅读全文
posted @ 2013-05-20 09:44 tanghulu321 阅读(165) 评论(0) 推荐(0) 编辑
摘要: 1 class Solution { 2 public: 3 int maxSubArray(int A[], int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int result; 7 if (n == 0) return result; 8 9 vector <int> record(n,0);10 record[0] = A[0]... 阅读全文
posted @ 2013-05-14 02:08 tanghulu321 阅读(94) 评论(0) 推荐(0) 编辑
摘要: 1 class Solution { 2 public: 3 int lengthOfLongestSubstring(string s) { 4 int n = s.length(); 5 int i = 0, j = 0; 6 int maxLen = 0; 7 bool exist[256] = { false }; 8 while (j < n) { 9 if (exist[s[j]]) {10 maxLen = max(maxLen, j-i);11 while (s[i... 阅读全文
posted @ 2013-05-14 01:57 tanghulu321 阅读(85) 评论(0) 推荐(0) 编辑
摘要: 思路: 这道题比较难,有一个解释很好:Brute force solution, O(N3):The obvious brute force solution is to pick all possible starting and ending positions for a substring, and verify if it is a palindrome. There are a total of C(N, 2) such substrings (excluding the trivial solution where a character itself is a palindro 阅读全文
posted @ 2013-05-13 09:49 tanghulu321 阅读(130) 评论(0) 推荐(0) 编辑