可以仅使用 std::sort() 将零移动到数组的末尾
possible to move zeros to the end of the array using only std::sort()?
我正在研究这个问题:
给定一个数组 nums,编写一个函数将所有 0 移动到它的末尾,同时保持非零元素的相对顺序。
我知道如何通过进行就地交换来回答这个问题,但我也想看看是否有可能用std::sort
来解决它。
根据 cplusplus.com:
排序函数的比较器函数是一个二进制函数,它接受范围内的两个元素作为参数,并返回可转换为布尔值的值。返回的值指示作为第一个参数传递的元素是否被视为在其定义的特定严格弱排序中先于第二个参数。
该函数不得修改其任何参数。
这可以是函数指针或函数对象。
//comments below are based on my understanding
static bool comp(int a, int b){
//lambda function evaluates to true - no swap,
//evaluates to false -swap
if(a==0) return false;
if(b==0) return true;
//if neither a nor b is 0 them do not swap
return true;
}
void moveZeroes(vector<int>& nums) {
sort(nums.begin(),nums.end(),comp);
}
给定的测试用例是[0,1,0,3,12]
我的输出是[12,3,1,0,0]
你几乎做对了。在比较器函数中,您必须返回false
才能不交换它们。此外,将std::sort
更改为std::stable_sort
以保持值的原始顺序。
static bool comp(int a, int b)
{
//lambda function evaluates to true - no swap,
//evaluates to false -swap
if(a==0) return false;
if(b==0) return true;
//if neither a nor b is 0 them do not swap
return false;
}
void moveZeros(std::vector<int>& nums)
{
std::stable_sort(nums.begin(),nums.end(),comp);
}
现场演示
正如Drew Dormann指出的那样,稳定分区是正确的算法。这是代码:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> data { 0, 1, 0, 3, 12 };
std::stable_partition(
data.begin(), data.end(), [](int n) { return n != 0; });
for (auto i : data)
cout << i << ' ';
cout << endl;
}
输出1 3 12 0 0
要使用的排序顺序只是零比所有非零值"大",并且等于其他零。 所有其他非零值都"小于"零,并且等效于任何其他非零值。
正确构造比较函数,然后您可以在调用std::stable_sort
中使用它来实现您要做的事情。
相关文章:
- 移动二维数组中的字符
- 新的整数数组向右移动?
- 当 std::move 与 C 样式数组或不移动对象时会发生什么
- 在数组中输入 n 个整数的列表,并以类似于钟摆来回移动的方式排列它们. 输入-1 3 2 5 4,输出5 3 1 2 4
- 数组指针表示法C++(移动数组时)
- C++ - 块不移动,我的数组不起作用
- 如何将数组中的元素向右移动?
- 如何将不可移动和不可复制的函数返回值获取到数组中
- 尝试通过多个函数移动数组,但遇到了我不知道如何修复的错误
- 移动数组C++中的特定元素
- 在 C++ 中移动数组元素
- C++移动数组元素的正确方法aligned_storage
- 移动数组指针未更改开始的地址
- 使用memcpy移动数组并在给定位置插入一个新元素
- 如何移动数组中的元素,将奇数放到数组的开头(从最小到最大),甚至放到后面(从最大到最小)
- 单独移动数组(raw array, std::array, std::vector)的每个元素
- 向下移动数组值1
- c++中移动数组的函数
- 移动数组中字符串的位置
- 移动数组元素