diff --git a/solution/0100-0199/0137.Single Number II/README.md b/solution/0100-0199/0137.Single Number II/README.md index 98278806bd4b0..89148166fa930 100644 --- a/solution/0100-0199/0137.Single Number II/README.md +++ b/solution/0100-0199/0137.Single Number II/README.md @@ -122,6 +122,26 @@ public: }; ``` +### **Swift** + +```swift +class Solution { + func singleNumber(_ nums: [Int]) -> Int { + var a = nums.sorted() + var n = a.count + for i in stride(from: 0, through: n - 1, by: 3) { + if i == n - 1 { + return a[i] + } + else if a[i] != a[i + 1] { + return a[i] + } + } + return 0 + } +} +``` + ### **...** ``` diff --git a/solution/0100-0199/0137.Single Number II/README_EN.md b/solution/0100-0199/0137.Single Number II/README_EN.md index 4402c2590b814..7a02a41565b17 100644 --- a/solution/0100-0199/0137.Single Number II/README_EN.md +++ b/solution/0100-0199/0137.Single Number II/README_EN.md @@ -101,6 +101,26 @@ public: }; ``` +### **Swift** + +```swift +class Solution { + func singleNumber(_ nums: [Int]) -> Int { + var a = nums.sorted() + var n = a.count + for i in stride(from: 0, through: n - 1, by: 3) { + if i == n - 1 { + return a[i] + } + else if a[i] != a[i + 1] { + return a[i] + } + } + return 0 + } +} +``` + ### **...** ``` diff --git a/solution/0100-0199/0137.Single Number II/Solution.swift b/solution/0100-0199/0137.Single Number II/Solution.swift new file mode 100644 index 0000000000000..bcb3a546fcfb0 --- /dev/null +++ b/solution/0100-0199/0137.Single Number II/Solution.swift @@ -0,0 +1,15 @@ +class Solution { + func singleNumber(_ nums: [Int]) -> Int { + var a = nums.sorted() + var n = a.count + for i in stride(from: 0, through: n - 1, by: 3) { + if i == n - 1 { + return a[i] + } + else if a[i] != a[i + 1] { + return a[i] + } + } + return 0 + } +} \ No newline at end of file