반응형
배타적 논리합, XOR 연산에 관한 문제다.
초기값만 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;
}
};
반응형
'IT > 알고리즘' 카테고리의 다른 글
14] Leetcode 1221. Split a String in Balanced Strings (0) | 2020.06.25 |
---|---|
13] Leetcode 709. To Lower Case (0) | 2020.06.25 |
11] Leetcode 1295. Find Numbers with Even Number of Digits (0) | 2020.06.24 |
10] Leetcode 1389. Create Target Array in the Given Order (0) | 2020.06.23 |
9] Leetcode 1281. Subtract the Product and Sum of Digits of an Integer (0) | 2020.06.22 |
댓글