当前位置:网站首页>2022/7/19
2022/7/19
2022-07-21 01:03:00 【anieoo】
The finger of the sword Offer 61. Shunzi in playing cards
class Solution {
public:
bool isStraight(vector<int>& nums) {
sort(nums.begin(), nums.end()); // Sort
int joker = 0;
for(auto &x : nums) {
if(x == 0) joker++;
else break;
}
if(joker >= 4) return true; //4 More than two big and small Wang directly become shunzi
for(int i = joker + 1;i < 5;i++) {
int diff = nums[i] - nums[i - 1];
if(diff == 0) return false; // The difference between before and after is 0, return false
else if(diff == 1) continue;
else { // Use size Wang to offset the difference
joker -= (diff - 1);
if(joker < 0) return false;
}
}
return true;
}
};
The finger of the sword Offer 62. The last number in the circle
Joseph Ring problem
class Solution {
public:
int lastRemaining(int n, int m) {
int ans = 0;
for(int i = 2;i <= n;i++) {
ans = (ans + m) % i;
}
return ans;
}
};
class Solution {
public:
vector<int> constructArr(vector<int>& a) {
vector<int> res(a.size(), 1);
for(int i = 1;i < a.size();i++) { // Traversing from front to back , Calculation a[i] Product of previous
res[i] = res[i - 1] * a[i - 1];
}
int s = 1;
for(int j = a.size() - 1;j >= 0;j--) { // Calculation a[i] Product after
res[j] *= s;
s *= a[j];
}
return res;
}
};
The original intention of the topic is to find the first val stay p and q The node between .
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == NULL) return NULL;
if(root->val > p->val && root->val > q->val) return lowestCommonAncestor(root->left, p, q);
if(root->val < p->val && root->val < q->val) return lowestCommonAncestor(root->right, p, q);
return root;
}
};
/**
* 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* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == NULL || root == p || root == q) return root;
TreeNode *left = lowestCommonAncestor(root->left, p, q);
TreeNode *right = lowestCommonAncestor(root->right, p, q);
if(left != NULL && right != NULL) return root;
else return left == NULL ? right : left;
}
};
class Solution {
public:
vector<int> getLeastNumbers(vector<int>& arr, int k) {
priority_queue<int, vector<int>, greater<int>> heap;
for(auto &x : arr) heap.push(x);
vector<int> res;
for(int i = 0;i < k;i++) {
res.push_back(heap.top());
heap.pop();
}
return res;
}
};
边栏推荐
- 容易被忽视的五个安全环节,比想象中的更危险!
- [JVM series] common garbage collector in JVM
- QT_嵌入式主界面添加与切换效果
- How to get started NET Core ? Recommend these 10 excellent open source projects!
- 哈希表(HashTable)
- CCTV news news "Suzhou restaurant manual tearing quota invoice" news channel_ People's network
- Video playback
- 【JVM 系列】JVM 对象的分配策略
- Hollysys PLC master-slave station communication
- 华为(huawei)园区网的常规部署配置示例
猜你喜欢
随机推荐
【2022国赛模拟】相似序列问题——DFA(DP套DP)
这款国产良心软件正式开源!
CCTV news "Hangzhou rent quota invoice by hand" news channel_ People's network
CCTV news news "Beijing opens catering manual tearing quota invoice" news channel_ People's network
零信任和SASE有什么不一样?答案其实并不重要
LeetCode_90_子集Ⅱ
Hollysys PLC master-slave station communication
液晶ST7302图像缓存结构
Speech signal processing: sampling theorem [as long as the sampling frequency is greater than or equal to twice the maximum frequency of the effective signal, the sampling value can contain all the in
uview錶單,身份證,鍵盤,實時驗證
Recommend an open source mall
CCTV news "Ningbo rent quota invoice by hand" news channel_ People's network
C # understand these 100 + lines of code, and you will really get started (Classic)
建模杂谈系列144 SCLC工程化实验
[JVM series] common garbage collector in JVM
我想在label标签上显示SQL数据库表里的记录数
性能监控 之 Prometheus 三剑客安装案例
ViT【backbone】
Popular understanding of tensor
ADG备库可以进行数据泵导出吗?不可以