随笔分类 - Algorithm
classic algorithm, problem
    
摘要:class Solution {private: int sum;public: int sumNumbers(TreeNode *root) { sum = 0; dfs(root, 0); return sum; } vo...
        阅读全文
                
摘要:typedef pair P;class Solution {public: int _candy(vector &ratings) { int len = ratings.size(); if (len == 0) return 0; ...
        阅读全文
                
摘要:class Solution {public: ListNode *sortList(ListNode *head) { if (head == NULL || head->next == NULL) return head; ListNode *h1 = NULL...
        阅读全文
                
摘要:class Solution {public: ListNode *deleteDuplicates(ListNode *head) { if (head == NULL || head->next == NULL) return head; Lis...
        阅读全文
                
摘要:class Solution {public: ListNode *deleteDuplicates(ListNode *head) { if (head == NULL) return NULL; ListNode *pre, *cur, *tmp; ...
        阅读全文
                
摘要:class Solution {public: ListNode *rotateRight(ListNode *head, int k) { if (head == NULL) return NULL; ListNode *p = head, *q = head; ...
        阅读全文
                
摘要:class Solution {public: int divide(int dividend, int divisor) { bool aneg = dividend a || a == 0) return 0; unsigned int abits = 0, ...
        阅读全文
                
摘要:class Solution {public: vector > threeSum(vector &num) { vector > res; int len = num.size(); sort(num.begin(), num.end()); ...
        阅读全文
                
摘要:先来一个暴力解:class Solution {public: vector > fourSum(vector &num, int target) { vector > res; vector r; r.resize(4); sort(n...
        阅读全文
                
摘要:class UnionFindSet { private: int *pref; int *rank; int capacity; public: UnionFindSet(int n) { if (n = c...
        阅读全文
                
摘要:#include #include #include #include using namespace std;typedef pair P;int main() { // zero means no connection int graph[5][5] = { ...
        阅读全文
                
摘要:单源最短路径算法Bellman-ford练习,可以处理有负边的情况(也可以在存在负圈时及时终止)#include #include #include using namespace std;class Edge {public: int from; int to; int cost...
        阅读全文
                
摘要:#include #include using namespace std;class BinaryIndexedTree {private: int *mem; int capacity;public: BinaryIndexedTree (int n) { if ...
        阅读全文
                
摘要:class SegmentTree { private: int *mem; int *idx; int capacity; int storage_size; private: void init_level_upd...
        阅读全文
                
摘要:早就听人提起过线段树,今天有题搞不出来,讨论上说要用一下线段树,看了下,本质上是空间划分索引,只不过是一维上面的,如果在二维则是四叉树,三维则是八叉树,如果可以动态调整那么跟R-Tree就很相似了,他们都可以对范围查询做出响应。参照书上写了一个,虽然不多,但是渣渣也写的很是费力#include #i...
        阅读全文
                
摘要:1 class Solution { 2 public: 3 bool isValid(string s) { 4 char open[] = {'(', '[', '{'}; 5 char close[]= {')', ']', '}'}; 6 ...
        阅读全文
                
摘要:1 class Solution { 2 public: 3 vector > subsets(vector &S) { 4 sort(S.begin(), S.end()); 5 vector > res; 6 vector path; 7...
        阅读全文
                
摘要: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 ...
        阅读全文
                
摘要: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...
        阅读全文
                
摘要:class Solution {public: void reverseWords(string &s) { int len = s.length(); reverse(s.begin(), s.end()); int wpos = 0; ...
        阅读全文
                
 
                    
                
 
         浙公网安备 33010602011771号
浙公网安备 33010602011771号