136. Single Number

LeetCode easy original: C# #array #csharp #easy #graph #leetcode
선택한 UI 언어에 맞게 문제 텍스트를 러시아어에서 번역합니다. 코드는 변경하지 않습니다.

Дан непустой 배열 целых чисел nums, в котором каждый element встречается дважды, кроме одного. find этот единственный element.

C# 해법

매칭됨/원본
public class Solution {
    public int SingleNumber(int[] nums) {
        List<int> no_duplicate_list = new List<int>();
        foreach (int i in nums) {
            if (!no_duplicate_list.Contains(i)) {
                no_duplicate_list.Add(i);
            } else {
                no_duplicate_list.Remove(i);
            }
        }
        return no_duplicate_list[0];
    }
}

C++ 해법

자동 초안, 제출 전 검토
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    public int SingleNumber(vector<int>& nums) {
        List<int> no_duplicate_list = new List<int>();
        foreach (int i in nums) {
            if (!no_duplicate_list.Contains(i)) {
                no_duplicate_list.push_back(i);
            } else {
                no_duplicate_list.Remove(i);
            }
        }
        return no_duplicate_list[0];
    }
}

Java 해법

매칭됨/원본
class Solution {
    public int singleNumber(int[] nums) {
        List<Integer> no_duplicate_list = new ArrayList<>();

        for (int i : nums) {
            if (!no_duplicate_list.contains(i)) {
                no_duplicate_list.add(i);
            } else {
                no_duplicate_list.remove(new Integer(i));
            }
        }
        return no_duplicate_list.get(0);
    }
}

JavaScript 해법

매칭됨/원본
var singleNumber = function (nums) {
    var no_duplicate_list = [];
    for (var i of nums) {
        if (!no_duplicate_list.includes(i)) {
            no_duplicate_list.push(i);
        } else {
            no_duplicate_list.splice(no_duplicate_list.indexOf(i), 1);
        }
    }
    return no_duplicate_list[0];
};

Python 해법

매칭됨/원본
class Solution(object):
    def singleNumber(self, nums: List[int]) -> int:
        no_duplicate_list = []
        for i in nums:
            if i not in no_duplicate_list:
                no_duplicate_list.append(i)
            else:
                no_duplicate_list.remove(i)
        return no_duplicate_list.pop()

Go 해법

매칭됨/원본
func singleNumber(nums []int) int {
    no_duplicate_list := []int{}
    for _, i := range nums {
        found := false
        for j, x := range no_duplicate_list {
            if i == x {
                found = true
                no_duplicate_list = append(
                    no_duplicate_list[:j],
                    no_duplicate_list[j+1:]...)
                break
            }
        }
        if !found {
            no_duplicate_list = append(no_duplicate_list, i)
        }
    }
    return no_duplicate_list[0]
}

Vacancies for this task

활성 채용 with overlapping task tags are 표시됨.

전체 채용
아직 활성 채용이 없습니다.