随笔分类 -  Algorithm

classic algorithm, problem
摘要:1 class Solution { 2 public: 3 string longestCommonPrefix(vector &strs) { 4 if (strs.size() == 0) return ""; 5 string commstr = s... 阅读全文
posted @ 2014-04-03 00:44 卖程序的小歪 阅读(140) 评论(0) 推荐(0)
摘要:1 class LRUCache{ 2 private: 3 int max_size; 4 unordered_map >::iterator> hash; 5 list > lru_stack; 6 public: 7 LRUCache(int capacity... 阅读全文
posted @ 2014-04-02 22:29 卖程序的小歪 阅读(193) 评论(0) 推荐(0)
摘要:1 class Solution { 2 public: 3 bool isSymmetric(TreeNode *root) { 4 if (root == NULL) return true; 5 return dfs(root->left, root-... 阅读全文
posted @ 2014-04-01 16:05 卖程序的小歪 阅读(154) 评论(0) 推荐(0)
摘要:1 class Solution { 2 public: 3 int minCut(string s) { 4 int len = s.length(); 5 int* p_dp = new int[len + 1]; 6 char* s_dp = new char[len * len]; 7 int* mm = new int[len + 1]; 8 mm[0] = 0; 9 memset(s_dp, -1, len * len);10 memset(p_dp, 0,... 阅读全文
posted @ 2014-03-27 01:10 卖程序的小歪 阅读(175) 评论(0) 推荐(0)
摘要:1 class Solution { 2 public: 3 vector > partition(string s) { 4 int len = s.length(); 5 vector >* memo = new vector >[len + 1]; 6 vector > ret; 7 8 for (int i=1; i >& cur = memo[i];11 12 for (int j=i-1; j>=0; j--) {13 ... 阅读全文
posted @ 2014-03-26 20:48 卖程序的小歪 阅读(208) 评论(0) 推荐(0)
摘要:class Solution {public: int removeDuplicates(int A[], int n) { if (n == 0) return 0; int *rpos = A, *wpos = A + 1, *end = A + n; int cur; int pre = *(rpos++); while (rpos != end) { cur = *(rpos++); if (pre == cur) continue; *(wpos++) = pre = cur; } retur... 阅读全文
posted @ 2014-03-26 00:09 卖程序的小歪 阅读(92) 评论(0) 推荐(0)
摘要:1 class Solution { 2 public: 3 string addBinary(string a, string b) { 4 string res; 5 int carry = 0, s = 0; 6 int apos = a.length() - 1; 7 int bpos = b.length() - 1; 8 int astp = 0, bstp = 0; 9 // skip leading zero(s)10 for (; astp = astp,... 阅读全文
posted @ 2014-03-25 00:48 卖程序的小歪 阅读(159) 评论(0) 推荐(0)
摘要:1 class Solution { 2 public: 3 int numTrees(int n) { 4 return dfs(1, n); 5 } 6 7 int dfs(int start, int end) { 8 if ... 阅读全文
posted @ 2014-03-24 23:53 卖程序的小歪 阅读(168) 评论(0) 推荐(0)
摘要:class Solution {public: // verbose one vector insert(vector &intervals, Interval newInterval) { vector ret; int ns = newInterval.s... 阅读全文
posted @ 2014-03-24 18:50 卖程序的小歪 阅读(202) 评论(0) 推荐(0)
摘要:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool hasPathSum(TreeNode *root, int sum) { return dfs(root, 0, sum); } bool d... 阅读全文
posted @ 2014-03-24 16:06 卖程序的小歪 阅读(158) 评论(0) 推荐(0)
摘要:class Solution {public: vector findSubstring(string S, vector &L) { vector res; unordered_map stat; unordered_map run; int len = S.size(); int num = L.size(); int per = 0; if (num == 0 || !(per = L[0].size())) return res; int part= num * per; if (part > len) return res; ... 阅读全文
posted @ 2014-03-22 14:41 卖程序的小歪 阅读(337) 评论(0) 推荐(0)
摘要:class Solution {public: int maxPathSum(TreeNode *root) { int s, m; dfs(root, s, m); return (m > s) ? m : s; } void d... 阅读全文
posted @ 2014-03-22 01:26 卖程序的小歪 阅读(272) 评论(0) 推荐(0)
摘要:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector preorderTraversal(TreeNode *root) { vector res; if (root == NULL) return ... 阅读全文
posted @ 2014-03-21 14:28 卖程序的小歪 阅读(140) 评论(0) 推荐(0)
摘要:class Solution {public: vector inorderTraversal(TreeNode *root) { vector res; if (root == NULL) return res; vector > stack; ... 阅读全文
posted @ 2014-03-21 12:53 卖程序的小歪 阅读(228) 评论(0) 推荐(0)
摘要:class Solution {public: vector twoSum(vector &numbers, int target) { vector ret; vector > nums; for (int i=0; i target) { ... 阅读全文
posted @ 2014-03-21 02:41 卖程序的小歪 阅读(209) 评论(0) 推荐(0)
摘要:class Solution {public: int minimumTotal(vector > &triangle) { if (!triangle.size() || !triangle[0].size()) return 0; int row... 阅读全文
posted @ 2014-03-21 02:06 卖程序的小歪 阅读(133) 评论(0) 推荐(0)
摘要:typedef struct tagCharUsed { int i; int j; bool used; tagCharUsed(int _i, int _j, bool _used = false): i(_i), j(_j), used(_used){};} CharU... 阅读全文
posted @ 2014-03-20 15:23 卖程序的小歪 阅读(211) 评论(0) 推荐(0)
摘要:Given a stringSand a stringT, count the number of distinct subsequences ofTinS.A subsequence of a string is a new string which is formed from the orig... 阅读全文
posted @ 2014-03-19 23:57 卖程序的小歪 阅读(173) 评论(0) 推荐(0)
摘要:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(... 阅读全文
posted @ 2014-03-19 01:23 卖程序的小歪 阅读(161) 评论(0) 推荐(0)
摘要:class Solution {public: int minDepth(TreeNode *root) { if (root == NULL) return 0; int min_depth = INT_MAX; dfs(root, 0, min_d... 阅读全文
posted @ 2014-03-18 16:15 卖程序的小歪 阅读(180) 评论(0) 推荐(0)