加密数字
1 | class Solution { |
最小公共区域
1 | class Solution { |
近义词句子
1 | vector<string>res, words; |
不相交的握手
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | vector<string>res, words; |
1 | class Solution { |
1 | class Leaderboard { |
1 | class Solution { |
树的直径的解法,两次 BFS
1 | class Solution { |
1 | class Solution { |
1 | int missingNumber(vector<int>& arr) { |
1 | vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) { |
1 | double probabilityOfHeads(vector<double>& prob, int target) { |
1 | int maximizeSweetness(vector<int>& a, int K) { |
1 | class Solution { |
1 | /** |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
Exactly K times = at most K times - at most K - 1 times
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | /* |
Gray code
https://cp-algorithms.com/algebra/gray-code.html
i ^ i >> 1 生成格雷码
start ^ i ^ i >> 1 保证第一个和最后一个 只差一个bit1
2
3
4
5
6
7
8
9class Solution {
public:
vector<int> circularPermutation(int n, int start) {
vector<int> res;
for (int i = 0; i < 1 << n; ++i)
res.push_back(start ^ i ^ i >> 1);
return res;
}
};
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | bool cmp(string& a, string& b){ |
1 | class Solution { |
1 | class Solution { |
Example:
1 | Input: 3 |
1 | /** |
dp:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* CopyTree(TreeNode* root){
if(!root) return NULL;
TreeNode* newroot = new TreeNode(root->val);
newroot->left = CopyTree(root->left);
newroot->right = CopyTree(root->right);
return newroot;
}
vector<TreeNode*> generateTrees(int n) {
vector<TreeNode*> pre;
pre.push_back({nullptr});
if(n <= 0) return {};
vector<TreeNode*> cur;
for(int i=1; i<=n; i++){
for(TreeNode* node: pre){
TreeNode* root = new TreeNode(i);
root->left = node;
cur.push_back(root);
TreeNode* temp = node;
while(temp){
TreeNode* newroot = new TreeNode(i);
if(temp->right){
TreeNode* node_right = temp->right;
newroot->left = temp->right;
temp->right = newroot;
TreeNode* newtree = CopyTree(node);
temp->right = node_right;
temp = temp->right;
cur.push_back(newtree);
}
else{
temp->right = newroot;
TreeNode* newtree = CopyTree(node);
cur.push_back(newtree);
temp->right = nullptr;
temp = temp->right;
}
delete newroot;
}
}
pre = cur;
cur.clear();
}
return pre;
}
};
Given a nested list of integers, implement an iterator to flatten it.
Each element is either an integer, or a list – whose elements may also be integers or other lists.
1 | Example 1: |
1 | /** |