当前位置:网站首页>leetcode18. Sum of four numbers
leetcode18. Sum of four numbers
2022-04-21 19:36:00 【Every day up the dish chicken Jie!!】
One : subject

Two : Upper code
class Solution {
public:
/** Ideas : 1. The difference between this and the sum of three is that this question adds a layer of circulation , That is, we add the second layer loop after the first element we traverse 2. Other ideas are the same as the sum of three */
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int> >ans;
sort(nums.begin(),nums.end());
for (int i = 0; i < nums.size(); i++) {
if (i > 0 && nums[i] == nums[i-1]) continue;// To reprocess
// It can't be nums[i] = nums[i+1]
// Because then we'll miss it -1 -1 1 1 taget = 0
for (int j = i+1; j < nums.size(); j++) {
if ( j > i+1 && nums[j] == nums[j-1]) continue;
int left = j+1;
int right = nums.size()-1;
while (left < right) {
if (nums[i]+nums[j] > target - (nums[left]+nums[right])) {
right--;
} else if (nums[i]+nums[j] < target - (nums[left]+nums[right])) {
left++;
} else {
ans.push_back({
nums[i],nums[j],nums[left],nums[right]});
while (left < right && nums[left] == nums[left+1]) left++;
while (left < right && nums[right] == nums[right-1]) right--;
// The top de duplication is only the de duplication to the previous element of different elements .
left++;
right--;
}
}
}
}
return ans;
}
};
版权声明
本文为[Every day up the dish chicken Jie!!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211927129681.html
边栏推荐
- 影响VPS网站优化的因素有哪些?
- pfSense使用证书认证配置IPsec站点到站点隧道指南
- Questions fréquemment incorrectes pour la certification quotidienne du CISSP (20 avril 2022)
- Relationship between deep learning, multi machine and multi card batchsize and learning rate
- AAAI 2022 更细粒度的不实信息检测
- How to open the developer tool when the uniapp runs to the built-in browser
- Analyzing the practical process of maker Education
- Leetcode0886. Possible dichotomy (medium)
- Automatic control principle Chapter 6 - correction and synthesis of control system (mind map)
- 关系型数据库与非关系型数据库概述,Redis简介、常用命令及优化
猜你喜欢

【网络协议】ip addr

How to open the developer tool when the uniapp runs to the built-in browser
![[hand pose estimation] [paper interpretation] 3D human pose estimation in rgbd images for robotic task learning](/img/99/5c49b615839f3e8a036d9634371dce.png)
[hand pose estimation] [paper interpretation] 3D human pose estimation in rgbd images for robotic task learning

【手部姿态估计】【论文解读】3D Human Pose Estimation in RGBD Images for Robotic Task Learning

Sorting will be recursive. It's a pity not to learn non recursive

自动控制原理第6章——控制系统的校正及综合(思维导图)

使用mRemoteNG工具管理所有远程连接

The CPU and memory consumption are too high, but the memory and CPU occupied by the processes in the task manager cannot be seen
Learn MySQL performance tuning and make your database smooth

Instructions of crystal Chem mouse glucose detection kit
随机推荐
Comparison of F (ab ') 2 IgG isotype in abbexa goat
robotframework日志输出中文乱码
静态链接与动态链接
switch分支
面试官:简单聊聊 Go 逃逸分析?
MusicPlayer2.1版本
Analysis of service registration source code of Nacos
今天睡眠质量记录83分
RobotFramework部分关键字无法使用或关键字为黑色
配置PyTorch、TensorFlow 环境
OpenCV之OpenCL介绍
Conan软件包管理器的Qt 6.2.4软件包
Sorting will be recursive. It's a pity not to learn non recursive
Abbexa MPO (FITC) / CD3 (PE) combined antibody
Easygbs has closed the video recording plan. Why are video files still generated?
redis安装与配置开机自启
MySQL MHA高可用集群部署及故障切换
How does the national standard gb28181 platform easygbs turn on the voice intercom function?
自动控制原理第5章——频率法(思维导图)
C# 不能用于文件名的字符