随笔分类 - C++
摘要:1 int gcd(int a, int b) 2 { 3 return b ? gcd(b, a % b) : a; 4 }
阅读全文
摘要:1 class Solution 2 { 3 public: 4 //欧拉路径 5 vector<string> findItinerary(vector<vector<string>> &tickets) 6 { 7 unordered_map<string, multiset<string>>
阅读全文
摘要:给你一个数组 [2,1,2,4,3],你返回数组 [4,2,4,-1,-1]。 解释:第一个 2 后面比 2 大的数是 4; 1 后面比 1 大的数是 2;第二个 2 后面比 2 大的数是 4; 4 后面没有比 4 大的数,填 -1;3 后面没有比 3 大的数,填 -1。 1、右边第一个数比它大,从
阅读全文
摘要:1 struct cmp 2 { 3 bool operator()(pair<string,int> const &left,pair<string,int> const &right) 4 { 5 if(left.second == right.second) return left.first
阅读全文
摘要:1 class UnionFind 2 { 3 public: 4 int count; // 记录连通分量个数 5 vector<int> parent; // 存储若干棵树 6 vector<int> size; // 记录每棵树包含的节点数 7 8 UnionFind(int n) 9 { 1
阅读全文
摘要:1 class MyHashMap 2 { 3 public: 4 const static int N = 20011; 5 vector<list<pair<int,int>>> hash; 6 7 MyHashMap() 8 { 9 hash = vector<list<pair<int,in
阅读全文
摘要:1 //开放寻址法 2 class MyHashMap 3 { 4 public: 5 const static int N = 2000003; 6 int hash_key[N], hash_value[N]; 7 8 MyHashMap() 9 { 10 memset(hash_key, -1
阅读全文
摘要:括号序列合法 <==> 所有前缀和>=0,且总和等于0
阅读全文
摘要:1 // 将先修关系构成一张图,由每个数对的第二个数字向第一个数字连边。 2 // 首先将所有入度为0的点进队,准备拓扑排序。 3 // 宽搜过程中,将当前结点所关联的结点的入度减1;若发现新的入度为0的结点,则将其进队。 4 // 最后如果遍历了所有结点,则说明可以满足要求;否则,先修关系存在环。
阅读全文
摘要:1 class Solution 2 { 3 public: 4 int countPrimes(int n) 5 { 6 vector<bool> isPrime(n+1,true); //首先全部为true 7 int count = 0; 8 9 for(int i = 2; i < n;++
阅读全文
摘要:1 //删除重复数模板 2 class Solution 3 { 4 public: 5 int removeDuplicates(vector<int>& nums) 6 { 7 int i = 0; 8 for(int j = 0;j < nums.size();j ++) 9 if(i < k
阅读全文
摘要:1 void partition(int arr[], int l, int r, int num)//三路快排 2 { 3 int less = l - 1; 4 int more = r + 1; 5 int cur = 0; 6 while (cur < more) 7 { 8 if (arr
阅读全文
摘要:1 //所有实现最好写在一个类中 2 class Trie 3 { 4 public: 5 bool is_end; //是否以该单词结尾 6 Trie* son[26]; //该节点儿子的个数 7 Trie() 8 { 9 is_end = false; 10 for(int i = 0;i <
阅读全文
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),
阅读全文
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x),
阅读全文
摘要:1 //前序 2 void preOrderUnRecur(ListNode* head) 3 { 4 cout << "pre-order: "; 5 if (head != NULL) 6 { 7 stack<ListNode*> Stack; 8 Stack.push(head); 9 10
阅读全文
摘要:1 //spilt函数的实现 2 //假设以空格切分:char c = ' '; 3 class Solution 4 { 5 vector<string> res; 6 void spilt(string s,char c,vector<string> &res) 7 { 8 istringstr
阅读全文
摘要:1、全排列 1 class Solution 2 { 3 public: 4 int n; 5 vector<bool> st; 6 vector<vector<int>> ans; 7 vector<int> path; 8 9 vector<vector<int>> permute(vector
阅读全文
摘要:求解next数组 下标 0 1 2 3 4 a b a b c next -1 0 0 1 2 求解next数组时,下标为0,1已知,从下标为2开始 1 //KMP算法 2 class Solution 3 { 4 public: 5 //求next数组 6 vector<int> pre(stri
阅读全文

浙公网安备 33010602011771号