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 {
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}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments