-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_136_Single Number.swift
More file actions
77 lines (67 loc) · 1.72 KB
/
leetcode_136_Single Number.swift
File metadata and controls
77 lines (67 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// leetcode_136_Single Number.swift
// fight
//
// Created by 이재은 on 2020/11/27.
// Copyright © 2020 jaeeun. All rights reserved.
//
import Foundation
// LeetCode 136. Single Number
// 주어진 배열에서 하나의 숫자를 빼고 나머지는 모두 2개씩 들어있음
// 개수가 하나인 원소를 구하기
//Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
//
//Follow up: Could you implement a solution with a linear runtime complexity and without using extra memory?
//
//
//Example 1:
//
//Input: nums = [2,2,1]
//Output: 1
//Example 2:
//
//Input: nums = [4,1,2,1,2]
//Output: 4
//Example 3:
//
//Input: nums = [1]
//Output: 1
//
//Constraints:
//
//1 <= nums.length <= 3 * 10^4
//-3 * 10^4 <= nums[i] <= 3 * 10^4
//Each element in the array appears twice except for one element which appears only once.
// 풀이 1
func singleNumber(_ nums: [Int]) -> Int {
var numCount = [Int: Int]()
for n in nums {
numCount[n, default: 0] += 1
}
return numCount.filter { $0.value == 1 }.keys.compactMap { Int($0) }.first!
}
// 풀이 2
func singleNumber(_ nums: [Int]) -> Int {
nums.reduce(0, ^)
}
// 풀이 3
func SingleNumber(_ nums: [Int]) -> Int {
let doubleSumNumsSet = 2 * Set(nums).reduce(0, +)
let sumNums = nums.reduce(0, +)
return doubleSumNumsSet - sumNums
}
// 풀이 4
func singleNumber(_ nums: [Int]) -> Int {
var numsSet = Set<Int>()
for n in nums {
if numsSet.contains(n) {
numsSet.remove(n)
} else {
numsSet.insert(n)
}
}
return numsSet.first!
}
print(singleNumber([1])) // 1
print(singleNumber( [4,1,2,1,2])) // 4
print(singleNumber([2,2,1])) // 1