반응형

https://leetcode.com/problems/rearrange-array-elements-by-sign

 

LeetCode - The World's Leading Online Programming Learning Platform

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

 

nums를 순회하면서, 결과 배열에 양수와 음수를 순서에 맞게 삽입해주며 포인터를 업데이트해줍니다.

class Solution {
public:
    vector<int> rearrangeArray(vector<int>& nums) {
        vector<int> res(nums.size());
        int i = 0, j = 1;
        for(int num : nums) {
            if(num > 0) res[i] = num, i += 2;
            else res[j] = num, j += 2;
        }
        return res;
    }
};
반응형

+ Recent posts