From 56d19a317d9dac9bb1efdbd77d9d6adb3e3640e1 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Wed, 29 May 2024 07:54:18 +0100 Subject: [PATCH 1/2] Swift implementation for LCOF 56-I --- .../README.md" | 24 +++++++++++++++++++ .../Solution.swift" | 19 +++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23056 - I. \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/Solution.swift" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23056 - I. \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/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23056 - I. \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/README.md" index 84828af823293..49da74747d6b9 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23056 - I. \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/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23056 - I. \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/README.md" @@ -201,6 +201,30 @@ public class Solution { } ``` +#### Swift + +```swift +class Solution { + func singleNumbers(_ nums: [Int]) -> [Int] { + var xorSum = 0 + for num in nums { + xorSum ^= num + } + + let lowBit = xorSum & -xorSum + var a = 0 + for num in nums { + if (num & lowBit) != 0 { + a ^= num + } + } + + let b = xorSum ^ a + return [a, b] + } +} +``` + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23056 - I. \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/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23056 - I. \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/Solution.swift" new file mode 100644 index 0000000000000..6857d1a9f4073 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23056 - I. \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/Solution.swift" @@ -0,0 +1,19 @@ +class Solution { + func singleNumbers(_ nums: [Int]) -> [Int] { + var xorSum = 0 + for num in nums { + xorSum ^= num + } + + let lowBit = xorSum & -xorSum + var a = 0 + for num in nums { + if (num & lowBit) != 0 { + a ^= num + } + } + + let b = xorSum ^ a + return [a, b] + } +} \ No newline at end of file From c215e164ec2f9d1a84d90dff037a1e4baded4fc3 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 29 May 2024 07:50:19 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- .../README.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23056 - I. \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/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23056 - I. \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/README.md" index 49da74747d6b9..aa0ac0f2c4dcb 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23056 - I. \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/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23056 - I. \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/README.md" @@ -210,7 +210,7 @@ class Solution { for num in nums { xorSum ^= num } - + let lowBit = xorSum & -xorSum var a = 0 for num in nums { @@ -218,7 +218,7 @@ class Solution { a ^= num } } - + let b = xorSum ^ a return [a, b] }