본문 바로가기
IT/알고리즘

33] Leetcode 1304. Find N Unique Integers Sum up to Zero

by 깻잎쌈 2020. 7. 15.
반응형
 

Find N Unique Integers Sum up to Zero - 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

class Solution {
public:
    vector<int> sumZero(int n) {
        vector<int>ans;
        if(n%2 == 0){
            for(int i = 1;i<=n/2;i++){
                ans.push_back(i);
                ans.push_back(-i);
            }
        }
        else{
            ans.push_back(0);
            for(int i = 1;i<=n/2;i++){
                ans.push_back(i);
                ans.push_back(-i);
            }
        }
        
        return ans;
    }
};​

 

반응형

댓글