【C++】【LeetCode】50. Pow(x, n)
来源:互联网 发布:手机能注册淘宝邮箱吗 编辑:程序博客网 时间:2024/06/05 08:00
- Powx n
- 题目
- 思路
- 代码
Pow(x, n)
题目
Implement pow(x, n).
思路
pow(x, n),即x的n次方。通过递归,每次判断n是不是2的倍数,如果不是,减1后再除以2,如果n等于1或者-1,返回x或者1/x。
代码
class Solution {public: double myPow(double x, int n) { map<int, double> dic; double result; int flag; if (n == 0 || (x-1 < 0.00001 && x > 1)) { return 1; } result = helper(x, n); return result; } double helper(double x, int n) { while (true) { if (n == 1) { return x; } else if (n == -1) { return 1 / x; } if (n % 2 == 0) { double result = helper(x, n / 2); return result * result; } else { double result = helper(x, (n - 1) / 2); return result * result * x; } } return 0; }};
阅读全文
0 0
- LeetCode 50. Pow(x, n) with C
- 【C++】【LeetCode】50. Pow(x, n)
- [LeetCode]50.Pow(x, n)
- LeetCode --- 50. Pow(x, n)
- [Leetcode] 50. Pow(x, n)
- [leetcode] 50.Pow(x, n)
- LeetCode 50. Pow(x, n)
- 50. Pow(x, n) LeetCode
- [leetcode] 50. Pow(x, n)
- Leetcode 50. Pow(x, n)
- [LeetCode]50. Pow(x, n)
- LeetCode *** 50. Pow(x, n)
- LeetCode 50. Pow(x, n)
- LeetCode 50. Pow(x, n)
- 【LeetCode】50. Pow(x, n)
- [leetcode]50. Pow(x, n)
- LeetCode-50.Pow(x, n)
- LeetCode - 50. Pow(x, n)
- 【20170630】TensorFlow与python学习(1)-安装
- 基于selenium的自动化测试脚本编写-python
- <NOIP> 18 . P1308 统计单词数
- Redis单机及其集群的搭建
- 在Mac和Windows上使用grunt-scss-lint的问题
- 【C++】【LeetCode】50. Pow(x, n)
- 旷视科技(Face++)和孙剑博士近期一些研究工作总结
- 给定一个整形数组(10个元素)求出最大值。
- fastjson解析json数据
- 在SQL Server中创建用户角色及授权
- php生成二维码
- Lucene全文检索的基本原理
- 《你的灯亮着吗》
- epoll和select区别