From 0a64ee2cdce27135a2f4858e129c8e6542948cf8 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Thu, 9 May 2024 07:41:14 +0100 Subject: [PATCH] Swift Implementation for LCCI 17.01 --- lcci/17.01.Add Without Plus/README.md | 20 ++++++++++++++++++++ lcci/17.01.Add Without Plus/README_EN.md | 20 ++++++++++++++++++++ lcci/17.01.Add Without Plus/Solution.swift | 17 +++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 lcci/17.01.Add Without Plus/Solution.swift diff --git a/lcci/17.01.Add Without Plus/README.md b/lcci/17.01.Add Without Plus/README.md index dde30d25c3476..1d9eb73b4a840 100644 --- a/lcci/17.01.Add Without Plus/README.md +++ b/lcci/17.01.Add Without Plus/README.md @@ -43,6 +43,26 @@ class Solution { } ``` +```swift +class Solution { + func add(_ a: Int, _ b: Int) -> Int { + var a = a + var b = b + var sum = 0 + var carry = 0 + + while b != 0 { + sum = a ^ b + carry = (a & b) << 1 + a = sum + b = carry + } + + return a + } +} +``` + diff --git a/lcci/17.01.Add Without Plus/README_EN.md b/lcci/17.01.Add Without Plus/README_EN.md index 7b5d49f2e1a14..6d0e921362297 100644 --- a/lcci/17.01.Add Without Plus/README_EN.md +++ b/lcci/17.01.Add Without Plus/README_EN.md @@ -44,6 +44,26 @@ class Solution { } ``` +```swift +class Solution { + func add(_ a: Int, _ b: Int) -> Int { + var a = a + var b = b + var sum = 0 + var carry = 0 + + while b != 0 { + sum = a ^ b + carry = (a & b) << 1 + a = sum + b = carry + } + + return a + } +} +``` + diff --git a/lcci/17.01.Add Without Plus/Solution.swift b/lcci/17.01.Add Without Plus/Solution.swift new file mode 100644 index 0000000000000..98d5e885cebb2 --- /dev/null +++ b/lcci/17.01.Add Without Plus/Solution.swift @@ -0,0 +1,17 @@ +class Solution { + func add(_ a: Int, _ b: Int) -> Int { + var a = a + var b = b + var sum = 0 + var carry = 0 + + while b != 0 { + sum = a ^ b + carry = (a & b) << 1 + a = sum + b = carry + } + + return a + } +} \ No newline at end of file