当前位置:网站首页>力扣刷题02(三数之和+最大子序和+二叉树最近公共祖先)
力扣刷题02(三数之和+最大子序和+二叉树最近公共祖先)
2022-07-17 04:54:00 【sun*san】
三数之和

经典的求三数之和的题目,两数之和的升级版,该题如果用暴力的话会达到O(nnn)的时间复杂度,为了减少时间复杂度,我们可以用排序加双指针的方法, 先将数组排序一遍,从第一个数开始设为待定的第一个元素再设置两个双指针,l和r,l从第一个元素后面一 位开始遍历,r从最后一位开始,若大于负待定元素,则r向右移,反之则l向左移,知道两指针相遇,若相等 则加入答案数组中。
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>>ans;
if(nums.size()<3){
return ans;
}
sort(nums.begin(),nums.end());
for(int i=0;i<nums.size();i++){
if(i>0&&nums[i]==nums[i-1]) continue;
int p=nums[i];
for(int l=i+1,r=nums.size()-1;l<nums.size();l++){
if(l>i+1&&nums[l]==nums[l-1]) continue;
while(l<r&&nums[l]+nums[r]+p>0){
r--;
}
if(l==r) break;
if(nums[l]+nums[r]+p==0){
ans.push_back({nums[l],nums[r],p});
}
}
}
return ans;
}
};最大子序和

用动态规划写的
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int Max=nums[0];//Max初始值为第一个元素
int sum=nums[0];//sum值为更新与Max比较的值
for(int i=1;i<nums.size();i++){
if(i>0&&sum<0){//如果sum<0去掉之前的值,替换sum为当前值
sum=nums[i];
Max=max(Max,sum);
}
else if(i>0){//如果sum值>0与当前值相加,并且与Max值比较
sum+=nums[i];
Max=max(Max,sum);
}
cout<<sum<<endl;
}
return Max;
}
};买卖股票的最佳时机

改题虽然标的是简单标签,但还是看了题解才会做的T-T,一次遍历即可得出答案。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int Min=1e9;//为股票最低价
int Max=0;//注意这里不是股票最高价,是目前得到的最高利润
for(int i=0;i<prices.size();i++){//一次遍历交换数值
Min=min(Min,prices[i]);
Max=max(Max,prices[i]-Min);
}
return Max;
}
};二叉树的最近公共祖先

class Solution {
public:
TreeNode* ans;
bool dfs(TreeNode* root,TreeNode* p,TreeNode* q){
if(root==NULL){
return false;//如果一直搜索到空节点,则说明没有出现p,q两个节点,返回false
}
bool t1=dfs(root->left,p,q);//搜索左子树
bool t2=dfs(root->right,p,q);//搜索右子树
if ((t1 && t2) || ((root->val == p->val || root->val == q->val) && (t1 || t2))) {
//t1和t2都为真或者当前节点为p或q节点且t1或t2有一个为真则使答案为当前节点
ans = root;
}
return t1||t2||root->val==p->val||root->val==q->val;//当前遍历到p或q节点时,返回true
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root==NULL){
return ans;
}//特判root根为空的情况
dfs(root,p,q);//搜索
return ans;
}
};边栏推荐
- Real time Bi (IV) low cost data quasi real time processing idea
- Sphinx遇到的问题
- sql中的substr与substring函数用法
- HCR Huichen is walking on the north slope, a giant beast swimming into digital marketing services
- [Unity] 交互之双击
- OpenLDAP自定义schema
- [Unity] Input. Index of gettouch[index]
- Multiple connections will be maintained for each provider instance
- On the third day of security, iptables prevents nmap scanning and binlog
- [TA frost wolf _may - "hundred people plan"] art 2.1 DCC tool chain and engine tool chain
猜你喜欢

Usage scenarios and usage of judgment and rounding down in MySQL

HighTec 新建 AURIX TC37X demo 工程

MySQL表的约束(基础篇)

C语言动态内存开辟和柔性数组

Gin framework principle

TCP/IP 协议

HCR Huichen is walking on the north slope, a giant beast swimming into digital marketing services

一款好用的网络骗子举报系统无加密版本源码

【TA-霜狼_may-《百人计划》】图形2.5 Bump Mapping

如何进行mysql下的严格模式修改,使得使用插入用户表方式添加新用户成功
随机推荐
Efficient insertion of references in word with thousands of words and many pictures
Only when the data analysis report is written in this way can we really understand the data
Eureka,拿捏日千万级访问量妥妥的!
N-Beats模型于2020年发布,并且优于M4比赛的获胜者3%!
Autojs learning - realize blissful pure land
[Unity] 交互之双击
Unity UMP打包黑屏問題總結
Wkwebview sets the correct posture of custom useragent
Static attributes and static methods of class in JS
Cannot find module ‘process‘ or its corresponding type declarations.
C list set object de duplication LINQ de duplication with time de duplication
数据库学习笔记(一)检索数据
有望取代Deepfake?揭秘今年超火的NeRF技
Wechat applet source code of high imitation Netease cloud music UI
Sphinx遇到的问题
New network security architecture: Zero trust security
VB. Net plug-in development - extract files
golang反转reverse切片slice代码示例
[TA frost wolf _may - "hundred people plan"] art 2.1 DCC tool chain and engine tool chain
Analysis of network attack detection technology for NDN