当前位置:网站首页>406 double pointer (27. remove elements, 977. square of ordered array, 15. sum of three numbers, 18. sum of four numbers)
406 double pointer (27. remove elements, 977. square of ordered array, 15. sum of three numbers, 18. sum of four numbers)
2022-06-23 07:07:00 【liufeng2023】
27. Remove elements

class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int i = 0;
int j = 0;
for(; i < nums.size(); i++)
{
if(nums[i] == val)
{
continue;
}
nums[j++] = nums[i];
}
return j;
}
};

977. The square of an ordered array

class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
vector<int> v(nums.size(), 0);
for(int i = 0; i < nums.size(); i++)
{
v[i] = nums[i]*nums[i];
}
sort(v.begin(), v.end());
return v;
}
};

15. Sum of three numbers

class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); i++)
{
if (nums[i] > 0)
{
return result;
}
if (i > 0 && nums[i] == nums[i - 1])
{
continue;
}
int left = i + 1;
int right = nums.size() - 1;
while (right > left)
{
if (nums[i] + nums[left] + nums[right] > 0)
{
right--;
while (right > left && nums[right] == nums[right + 1]) right--;
}
else if (nums[i] + nums[left] + nums[right] < 0)
{
left++;
while (right > left && nums[left] == nums[left - 1]) left++;
}
else
{
result.push_back(vector<int>{
nums[i], nums[left], nums[right]});
// duplicate removal , stay i Without change , Only right-- and left++ It is possible to get the next sum 0 Array of ,nums Is increasing !
while (left < right && nums[right] == nums[right-1]) right--;
while (left < right && nums[left] == nums[left+1]) left++;
right--;
left++;
}
}
}
return result;
}
};

18. Sum of four numbers

class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> result;
sort(nums.begin(), nums.end());
//nums[k]
for (int k = 0; k < nums.size(); k++)
{
// Pruning treatment
if (nums[k] > target && (nums[k] >= 0 || target >= 0))
{
break;
}
// duplicate removal
if (k > 0 && nums[k] == nums[k - 1])
{
continue;
}
//nums[i]
for (int i = k + 1; i < nums.size(); i++)
{
// Secondary pruning treatment
if (nums[k] + nums[i] > target && (nums[k] + nums[i] >= 0 || target >= 0))
{
break;
}
// The right way to get rid of duplication
if (i > k + 1 && nums[i] == nums[i - 1])
{
continue;
}
int left = i + 1;
int right = nums.size() - 1;
while (right > left)
{
if (nums[k] + nums[i] > target - (nums[left] + nums[right]))
{
right--;
while (right > left && nums[right] == nums[right + 1]) right--;
}
else if (nums[k] + nums[i] < target - (nums[left] + nums[right]))
{
left++;
while (right > left && nums[left] == nums[left - 1]) left++;
}
else
{
result.push_back(vector<int>{
nums[k], nums[i], nums[left], nums[right]});
while (right > left && nums[left] == nums[left + 1]) left++;
while (right > left && nums[right] == nums[right - 1]) right--;
right--;
left++;
}
}
}
}
return result;
}
};

边栏推荐
猜你喜欢

Swagger3 integrates oauth2 authentication token

mysql 优化

Xiaobai must see in investment and wealth management: illustrated fund buying and selling rules

407-栈与队列(232.用栈实现队列、225. 用队列实现栈)

MySQL的意向共享锁、意向排它锁和死锁

图解三次握手四次挥手,小白都能看懂

MySQL Redo log Redo log

MySQL重做日志 redo log

How to migrate virtual machines from VirtualBox to hype-v
![[STL] unordered of associated container_ Map Usage Summary](/img/6a/d614f2f363fa5181c25e79ff8b0dab.png)
[STL] unordered of associated container_ Map Usage Summary
随机推荐
Linux安装mysql8.0.25
MySQL Redo log Redo log
Solve the mining virus sshd2 (redis does not set a password and clear the crontab scheduled task)
English grammar_ Adverb - ever / once
[system] right click the desktop icon. After turning around, the Explorer will crash and the desktop will be refreshed
What you need to know about five insurances and one fund
897. 递增顺序搜索树
The illustration shows three handshakes and four waves. Xiaobai can understand them
Page embedded iframe click browser back problem
Summarized benefits
【项目实训】线形箭头的变化
Xiaobai must see in investment and wealth management: illustrated fund buying and selling rules
301. 删除无效的括号
mysql 基础查询
994. 腐烂的橘子-非递归法
[STL] summary of pair usage
897. 递增顺序搜索树
QT method of compiling projects using multithreading
316. 去除重复字母
数据统计与分析基础 实验一 基本语法及运算