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

12] Leetcode 1486. XOR Operation in an Array

by 깻잎쌈 2020. 6. 24.
반응형

배타적 논리합, XOR 연산에 관한 문제다.

 

XOR Operation in an Array - 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

초기값만 start로 해주고 나머지는 이어서 고대로 해주면 된다.

class Solution {
public:
    int xorOperation(int n, int start) {
        int ans = start;
       for(int i = 1;i<n;i++)
            ans= ans^(start+2*i);       
        
        return ans;
    }
};

https://riptutorial.com/ko/cplusplus/example/8514/----%EB%B9%84%ED%8A%B8-xor--%EB%B0%B0%ED%83%80%EC%A0%81-%EB%85%BC%EB%A6%AC%ED%95%A9-

 

C++ - ^ - 비트 XOR (배타적 논리합) | c++ Tutorial

c++ documentation: ^ - 비트 XOR (배타적 논리합)

riptutorial.com

 

반응형

댓글