随笔分类 -  Algorithm

classic algorithm, problem
摘要:class Solution {private: int sum;public: int sumNumbers(TreeNode *root) { sum = 0; dfs(root, 0); return sum; } vo... 阅读全文
posted @ 2014-04-21 23:19 卖程序的小歪 阅读(135) 评论(0) 推荐(0)
摘要:typedef pair P;class Solution {public: int _candy(vector &ratings) { int len = ratings.size(); if (len == 0) return 0; ... 阅读全文
posted @ 2014-04-18 11:18 卖程序的小歪 阅读(190) 评论(0) 推荐(0)
摘要:class Solution {public: ListNode *sortList(ListNode *head) { if (head == NULL || head->next == NULL) return head; ListNode *h1 = NULL... 阅读全文
posted @ 2014-04-17 21:06 卖程序的小歪 阅读(168) 评论(0) 推荐(0)
摘要:class Solution {public: ListNode *deleteDuplicates(ListNode *head) { if (head == NULL || head->next == NULL) return head; Lis... 阅读全文
posted @ 2014-04-17 20:23 卖程序的小歪 阅读(123) 评论(0) 推荐(0)
摘要:class Solution {public: ListNode *deleteDuplicates(ListNode *head) { if (head == NULL) return NULL; ListNode *pre, *cur, *tmp; ... 阅读全文
posted @ 2014-04-17 19:31 卖程序的小歪 阅读(140) 评论(0) 推荐(0)
摘要:class Solution {public: ListNode *rotateRight(ListNode *head, int k) { if (head == NULL) return NULL; ListNode *p = head, *q = head; ... 阅读全文
posted @ 2014-04-17 16:55 卖程序的小歪 阅读(161) 评论(0) 推荐(0)
摘要:class Solution {public: int divide(int dividend, int divisor) { bool aneg = dividend a || a == 0) return 0; unsigned int abits = 0, ... 阅读全文
posted @ 2014-04-16 21:18 卖程序的小歪 阅读(183) 评论(0) 推荐(0)
摘要:class Solution {public: vector > threeSum(vector &num) { vector > res; int len = num.size(); sort(num.begin(), num.end()); ... 阅读全文
posted @ 2014-04-16 19:49 卖程序的小歪 阅读(137) 评论(0) 推荐(0)
摘要:先来一个暴力解:class Solution {public: vector > fourSum(vector &num, int target) { vector > res; vector r; r.resize(4); sort(n... 阅读全文
posted @ 2014-04-16 09:34 卖程序的小歪 阅读(156) 评论(0) 推荐(0)
摘要:class UnionFindSet { private: int *pref; int *rank; int capacity; public: UnionFindSet(int n) { if (n = c... 阅读全文
posted @ 2014-04-15 21:26 卖程序的小歪 阅读(172) 评论(0) 推荐(0)
摘要:#include #include #include #include using namespace std;typedef pair P;int main() { // zero means no connection int graph[5][5] = { ... 阅读全文
posted @ 2014-04-15 19:49 卖程序的小歪 阅读(182) 评论(0) 推荐(0)
摘要:单源最短路径算法Bellman-ford练习,可以处理有负边的情况(也可以在存在负圈时及时终止)#include #include #include using namespace std;class Edge {public: int from; int to; int cost... 阅读全文
posted @ 2014-04-15 19:45 卖程序的小歪 阅读(147) 评论(0) 推荐(0)
摘要:#include #include using namespace std;class BinaryIndexedTree {private: int *mem; int capacity;public: BinaryIndexedTree (int n) { if ... 阅读全文
posted @ 2014-04-15 16:11 卖程序的小歪 阅读(151) 评论(0) 推荐(0)
摘要:class SegmentTree { private: int *mem; int *idx; int capacity; int storage_size; private: void init_level_upd... 阅读全文
posted @ 2014-04-14 23:57 卖程序的小歪 阅读(175) 评论(0) 推荐(0)
摘要:早就听人提起过线段树,今天有题搞不出来,讨论上说要用一下线段树,看了下,本质上是空间划分索引,只不过是一维上面的,如果在二维则是四叉树,三维则是八叉树,如果可以动态调整那么跟R-Tree就很相似了,他们都可以对范围查询做出响应。参照书上写了一个,虽然不多,但是渣渣也写的很是费力#include #i... 阅读全文
posted @ 2014-04-14 19:07 卖程序的小歪 阅读(226) 评论(0) 推荐(0)
摘要:1 class Solution { 2 public: 3 bool isValid(string s) { 4 char open[] = {'(', '[', '{'}; 5 char close[]= {')', ']', '}'}; 6 ... 阅读全文
posted @ 2014-04-12 10:02 卖程序的小歪 阅读(137) 评论(0) 推荐(0)
摘要:1 class Solution { 2 public: 3 vector > subsets(vector &S) { 4 sort(S.begin(), S.end()); 5 vector > res; 6 vector path; 7... 阅读全文
posted @ 2014-04-12 09:27 卖程序的小歪 阅读(158) 评论(0) 推荐(0)
摘要:1 class Solution { 2 public: 3 bool searchMatrix(vector > &matrix, int target) { 4 int rows, cols; 5 if (!(rows = matrix.size()) || !(cols = matrix[0].size())) return false; 6 int ridx, cur, first = -1, last = rows; 7 while (first + 1 target) {13 ... 阅读全文
posted @ 2014-04-10 20:30 卖程序的小歪 阅读(196) 评论(0) 推荐(0)
摘要:1 class Solution { 2 public: 3 int removeDuplicates(int A[], int n) { 4 if (n <= 2) return n; 5 int wpos = 1; 6 int dups = 0; 7 int cur, last = A[0]; 8 9 for (int i=1; i<n; i++) {10 cur = A[i];11 dups = (cur == last) ? dup... 阅读全文
posted @ 2014-04-10 19:33 卖程序的小歪 阅读(130) 评论(0) 推荐(0)
摘要:class Solution {public: void reverseWords(string &s) { int len = s.length(); reverse(s.begin(), s.end()); int wpos = 0; ... 阅读全文
posted @ 2014-04-04 01:02 卖程序的小歪 阅读(224) 评论(0) 推荐(0)