내 맘대로 알고리즘/LeetCode 2020 May

May LeetCoding Challenge[Day12] - Single Element in a Sorted Array

You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once. Find this single element that appears only once.
Input: [3,3,7,7,10,11,11]
Output: 10

1. 알고리즘을 해결하기 위해서 생각한 방법

 

 - 쉬어가는 날인가보다.

 - 배열이 주어질 때, 배열 중에서 혼자 짝이 되지 않는 숫자를 찾는 문제이다.

 - 2020/04/20 - [내 맘대로 알고리즘] - LeetCode[day1] - Single Number

 - 4월에 첫 번째, Single Number라는 문제와 같은 문제이다.

 - 해당 문제를 풀기 위해서 ^ 연산자를 이해하고 있어야 한다.

 - XOR 연산자인 ^ 연산자는 00, 11 일 때는 0을 출력하고, 01 10 일 때는 1을 출력한다.

 - 00, 11 즉 같은 숫자가 2번 오게 되면 원래로 돌아오는 성질을 갖고 있고, 0A A0과 같이 한 번만 호출되면 A인 자기자신을 리턴한다.

 

2. 알고리즘 작성 중, 어려운 점

 

 - 없음!

 

3. 내가 작성한 알고리즘

 

class Solution {
    public int singleNonDuplicate(int[] nums) {
        int answer = 0;
        for(int i=0; i<nums.length; i++){
            answer ^= nums[i];
        }
        return answer;
    }
}

4. 내가 작성한 알고리즘 결과

 

 

 

문제 : https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/535/week-2-may-8th-may-14th/3327/

 

Explore - LeetCode

LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

leetcode.com