From 9e04c4b162fad5e397a6e767afeedb2908315c2b Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Tue, 21 May 2024 07:31:48 +0100 Subject: [PATCH 1/2] Swift Implementation for LCOF 16 --- .../README.md" | 26 +++++++++++++++++++ .../Solution.swift" | 21 +++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/Solution.swift" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" index 1808d49592873..63cdbfe50e0eb 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" @@ -237,6 +237,32 @@ public class Solution { } ``` +#### Swift + +```swift +class Solution { + func myPow(_ x: Double, _ n: Int) -> Double { + return n >= 0 ? qpow(x, Int64(n)) : 1 / qpow(x, -Int64(n)) + } + + private func qpow(_ a: Double, _ n: Int64) -> Double { + var ans: Double = 1 + var base: Double = a + var exponent: Int64 = n + + while exponent > 0 { + if (exponent & 1) == 1 { + ans *= base + } + base *= base + exponent >>= 1 + } + + return ans + } +} +``` + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/Solution.swift" new file mode 100644 index 0000000000000..da8c8e9ca3eca --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/Solution.swift" @@ -0,0 +1,21 @@ +class Solution { + func myPow(_ x: Double, _ n: Int) -> Double { + return n >= 0 ? qpow(x, Int64(n)) : 1 / qpow(x, -Int64(n)) + } + + private func qpow(_ a: Double, _ n: Int64) -> Double { + var ans: Double = 1 + var base: Double = a + var exponent: Int64 = n + + while exponent > 0 { + if (exponent & 1) == 1 { + ans *= base + } + base *= base + exponent >>= 1 + } + + return ans + } +} \ No newline at end of file From aab88e0e5d5813b7e3f6ec650566041bbee303f8 Mon Sep 17 00:00:00 2001 From: klever34 Date: Tue, 21 May 2024 06:35:25 +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\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" index 63cdbfe50e0eb..b57a8ca41dfa1 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" @@ -249,7 +249,7 @@ class Solution { var ans: Double = 1 var base: Double = a var exponent: Int64 = n - + while exponent > 0 { if (exponent & 1) == 1 { ans *= base @@ -257,7 +257,7 @@ class Solution { base *= base exponent >>= 1 } - + return ans } }