LeetCode OJ:Pow(x, n) (幂运算)
Implement pow(x, n).
幂运算,简单的方法snag然很好实现,直接循环相乘就可以了,但是这里应该不是那种那么简单,我的做法使用到了一点递归:
class Solution {
public:
double myPow(double x, int n)
{
if(n == INT_MIN)
return 1.0/(x * myPow(x, INT_MAX)); //这里考虑了一下由于传参数的限制还是将其变成INT_MAX
if(n < )
return 1.0/myPow(x, -n);
if(n == )
return 1.0;
double half = myPow(x, n/), ret = 0.0;
if(n%)
return half * half * x;
return half * half;
}
};
这里有个注意点就是考虑到INT_MIN的情况,做出相应的处理
下面是java写的版本:
class Solution {
public double myPow(double x, int n){
if(n == Integer.MIN_VALUE)
return 1.0/(x*myPow(x, Integer.MAX_VALUE));
if(n < 0)
return 1.0/myPow(x,-n);
if(n == 0)
return 1.0;
double half = myPow(x, n/2);
if(n%2 == 1)
return half * half * x;
return half * half;
}
}
LeetCode OJ:Pow(x, n) (幂运算)的更多相关文章
- LeetCode 50 Pow(x, n) (实现幂运算)
题目链接:https://leetcode.com/problems/powx-n/?tab=Description Problem:实现幂运算即 pow(x,n) 设形式为pow(x,n) ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 求幂运算、多项式乘法及Horner法则的应用
一,两种不同的求幂运算 求解x^n(x 的 n 次方) ①使用递归,代码如下: private static long pow(int x, int n){ if(n == 0) return 1; ...
- [ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)
Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael ...
- POJ1026 Cipher(置换的幂运算)
链接:http://poj.org/problem?id=1026 Cipher Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- 组合数学 - 置换群的幂运算 --- poj CARDS (洗牌机)
CARDS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1448 Accepted: 773 Description ...
- 迭代加深搜索 codevs 2541 幂运算
codevs 2541 幂运算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
随机推荐
- 调色盘canvas
//调色盘 function draw8(id){ var canvas = document.getElementById(id); var context = canvas.getContext( ...
- 错误:未启用当前数据库的SQL Server Service Broker,因此查询通知不受支持。如果希望使用通知,请为此数据库启用 Service Broker。
解决方法: 打开SQL Server,新建查询: ALTER DATABASE 数据库名 SET NEW_BROKER WITH ROLLBACK IMMEDIATE;ALTER DATABASE 数 ...
- poj3903 Stock Exchange 二分+dp
题目地址:http://poj.org/problem?id=3903 题目: Description The world financial crisis is quite a subject. S ...
- Linux安装字体文件
登录生成验证码的时候使用了monaco.ttf字体,没有安装字体的情况下,会报错:can't open resource Linux安装字体的方式其实很简单,就是调用fc-cache -f -v命令, ...
- this()必须放在构造方法的第一条
public class A { String name; int age; public A() { this("Jack",23); } public A(String nam ...
- 如何自定义echarts 线性图的选择事件
最近在做公司的数据大盘,要用到图表显示,echarts研二的时候有用过,我就决定用它了. 这里用到一个可以同时显示多条曲线的line-charts,基本样子如下: 看到这个画红色圈圈的地方了吗??? ...
- 解析Linux系统的平均负载概念
一.什么是系统平均负载(Load average)? 在Linux系统中,uptime.w.top等命令都会有系统平均负载load average的输出,那么什么是系统平均负载呢?系统平均负载被定义为 ...
- codeforces 357
C 题意: ###n个勇士编号1-n,m个回合对战,每个回合由仍留在游戏里的编号Li~Ri的人参加,胜者为Xi,输的人退出游戏. ###求一个a1-an的序列,若ai为胜者,则ai=0,否则ai=打败 ...
- JSR规范整理
Web Service技术 Java Date与Time API ( JSR 310) Java API for RESTful Web Services (JAX-RS) 1.1 (JSR 311) ...
- QTableWidget 列排序
connect(uirecord.tableWidget->horizontalHeader(),SIGNAL(sectionClicked(int)),this,SLOT(record_sor ...