Skip to content

Commit

Permalink
[Math] add Solution to Pow(x, n)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yi Gu committed Jun 21, 2016
1 parent ad01e5b commit b18753f
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Math/Pow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Question Link: https://leetcode.com/problems/powx-n/
* Primary idea: Classic Recursion and handle positive/negative case at first
*
* Time Complexity: O(logn), Space Complexity: O(1)
*/

class Pow {
func myPow(x: Double, _ n: Int) -> Double {
guard n != 0 else {
return 1
}
guard x != 0 else {
return 0
}

var res = _helper(abs(x), abs(n))

if n < 0 {
res = 1 / res
}
if n % 2 != 0 && x < 0 {
res = -res
}

return res
}

private func _helper(x: Double, _ n: Int) -> Double {
guard n != 0 else {
return 1
}
guard n != 1 else {
return x
}

if n % 2 == 0 {
return _helper(x * x, n / 2)
} else {
return _helper(x, n - 1) * x
}
}
}

0 comments on commit b18753f

Please sign in to comment.