注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
Input: An array of integers
Output: Modify in place the array so all the zeroes on are on the right. Return the number of zeroes
in the list.
Ex:
>> a = [1, 0, 3, 0, 4, 0, 5]
>> zero(a) // Returns 3
>> print a // [1, 4, 5, 3, 0, 0 ,0] Note that order doesn't have to be preserved
-> 1, 3, 4, 5, 0, 0, 0
int modifyArrayByZeroes(vector<int>& nums) {
if (nums.empty())
return 0;
int cnt = 0;
for (int i = 0; i < num.size(); i++) {
if (nums[i] == 0) {
cnt++;
}
else {
nums[i - cnt] = num3[i];
}
}
tes for i3
if (i3 + 1 < nums.size() && nums[i3] == nums[i3 + 1]) {
i3-;
continue;
}
int sum = nums[i1] + nums[i2] + nums[i3];
if (sum == 0)
return {nums[i1], nums[i2], nums[i3]};
else if (sum > 0) {
i3--;
}
else {
i2++;
}
}
}
return {};
} |