File tree Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -10,4 +10,21 @@ final class Solution {
10
10
func missingNumber( _ nums: [ Int ] ) -> Int {
11
11
( 0 ... nums. count) . reduce ( 0 , + ) - nums. reduce ( 0 , + )
12
12
}
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
+ }
13
30
}
Original file line number Diff line number Diff line change @@ -10,4 +10,15 @@ final class Solution {
10
10
func hammingWeight( _ n: Int ) -> Int {
11
11
n. nonzeroBitCount
12
12
}
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
+ }
13
24
}
You can’t perform that action at this time.
0 commit comments