美文网首页
50. Pow(x, n)

50. Pow(x, n)

作者: 寂灭天骄小童鞋 | 来源:发表于2020-03-18 13:53 被阅读0次

https://leetcode-cn.com/problems/powx-n/

func myPow(_ x: Double, _ n: Int) -> Double {
    var N = n
    var X = x
    if N < 0 {
        X = 1 / X
        N = -N
    }
    var result = 1.0
    var curNum = X
    
    while N > 0 {
        if N % 2 == 1 {
           result = curNum * result
        }
        curNum = curNum * curNum
        N = N / 2
    }
    return result
}

相关文章

网友评论

      本文标题:50. Pow(x, n)

      本文链接:https://www.haomeiwen.com/subject/jkwqyhtx.html