Skip to content

Commit 16eb8a0

Browse files
committed
add: Add other solutions
1 parent 0b7a4d1 commit 16eb8a0

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

missing-number/WhiteHyun.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,21 @@ final class Solution {
1010
func missingNumber(_ nums: [Int]) -> Int {
1111
(0 ... nums.count).reduce(0, +) - nums.reduce(0, +)
1212
}
13+
14+
func missingNumber2(_ nums: [Int]) -> Int {
15+
nums.count * (nums.count + 1) / 2 - nums.reduce(0, +)
16+
}
17+
18+
func missingNumber3(_ nums: [Int]) -> Int {
19+
Set(0...nums.count).subtracting(Set(nums)).first!
20+
}
21+
22+
func missingNumber4(_ nums: [Int]) -> Int {
23+
var answer = 0
24+
for i in 0 ..< nums.count {
25+
answer = answer ^ i ^ nums[i]
26+
}
27+
28+
return answer ^ nums.count
29+
}
1330
}

number-of-1-bits/WhiteHyun.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,15 @@ final class Solution {
1010
func hammingWeight(_ n: Int) -> Int {
1111
n.nonzeroBitCount
1212
}
13+
14+
func hammingWeight2(_ n: Int) -> Int {
15+
var number = n
16+
var answer = 0
17+
while number != 0 {
18+
answer += number & 1
19+
number >>= 1
20+
}
21+
22+
return answer
23+
}
1324
}

0 commit comments

Comments
 (0)