Algorithm/문제풀이
[LeetCode] 283. Move Zeroes 문제풀이
bluespacedev
2021. 9. 1. 18:59
https://leetcode.com/problems/move-zeroes/
Move Zeroes - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제 내용
배열에 0값이 섞여있는데 정렬상태를 유지하면서 0을 end쪽으로 몰아 넣는 문제
문제 풀이
index 2개로 swap하면 됐다.
class Solution {
public:
void moveZeroes(vector<int>& nums) {
for (int i = 0, j = 0; i < nums.size(); ++i) {
if (nums[i]) {
swap(nums[i], nums[j]);
++j;
}
}
}
};