50. Pow(x, n)
来源:互联网 发布:沈阳办公软件培训班 编辑:程序博客网 时间:2024/04/27 15:14
这道题目是求x的n次方。
可以选择用n个x相乘,复杂度为O(n)(负数的-n个相乘再求倒数)
另外就是采取分治来算
分解为pow(x,n/2)*pow(x,n/2)
最后的复杂度应该为logn+logn即为O(logn)
代码如下:
public double myPow(double x, int n) { if (n==0) { return 1; } double temp=myPow(x, n/2); if (n%2==0) { return temp*temp; } else { if (n>0) { return temp*temp*x; } else { return temp*temp/x; } } }
0 0
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50.Pow(x,n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- Android Studio 使用SVN的详细讲解
- VMware tool安装遇“正在进行简易安装时,无法手动启动VMware tools安装”问题的解决方案
- C#简单异步例子
- Java中多态的方法调用顺序问题
- sdutacm-字典树
- 50. Pow(x, n)
- 深入浅出Redis-Spring整合Redis
- android竞品分析之布局优化
- 蓝桥杯2012奇怪的比赛
- Lua 面向对象
- Leetcode——516. Longest Palindromic Subsequence
- 我的物联网综合知识之创建服务端1
- PopupWindow全屏显示以及适配不同手机屏幕之 应用实例 更换头像,拍照,相册选取附带动画效果
- 求指错,请高手指点,感谢