diff --git "a/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/README.md" index 43e176e618a64..199a56c1c4f20 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/README.md" @@ -202,6 +202,33 @@ public class Solution { } ``` +#### Swift + +```swift +class Solution { + func singleNumber(_ nums: [Int]) -> Int { + var bitCounts = [Int](repeating: 0, count: 32) + + for num in nums { + var x = num + for i in 0..<32 { + bitCounts[i] += x & 1 + x >>= 1 + } + } + + var result = 0 + for i in 0..<32 { + if bitCounts[i] % 3 == 1 { + result |= 1 << i + } + } + + return result + } +} +``` + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/Solution.swift" new file mode 100644 index 0000000000000..b0b0fbe1ecd38 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/Solution.swift" @@ -0,0 +1,22 @@ +class Solution { + func singleNumber(_ nums: [Int]) -> Int { + var bitCounts = [Int](repeating: 0, count: 32) + + for num in nums { + var x = num + for i in 0..<32 { + bitCounts[i] += x & 1 + x >>= 1 + } + } + + var result = 0 + for i in 0..<32 { + if bitCounts[i] % 3 == 1 { + result |= 1 << i + } + } + + return result + } +} \ No newline at end of file