leetcode 050 Pow(x, n)

来源:互联网 发布:大数据运用案例 编辑:程序博客网 时间:2024/05/17 06:39

Implement pow(x, n).Subscribe to see which companies asked this question



class Solution {public:    double myPow(double x, int n) {     double ans = 1.0;double temp = x;bool flag = false;long long cnt = n;if(cnt == 0) return 1.0;if(cnt < 0) {flag = true;cnt = -cnt;}while(cnt > 0) {if(cnt & 1) {ans *= temp;}temp *= temp;cnt = cnt >> 1;}return flag==true?1.0/ans:ans;    }};


0 0