【一天一道LeetCode】#50. Pow(x, n)
一天一道LeetCode系列
(一)题目
Implement pow(x, n).
(二)解题
题目很简单,实现x的n次方。
/*
需要注意一下几点:
1.n==0时,返回值为1
2.x==1时,返回值为1;x==-1时,根据n的奇偶来判断
3.n==-2147483648,特殊情况,int的范围时-2147483648~2147483647,
*/
class Solution {
public:
double myPow(double x, int n) {
if(n==0||x==1) return 1;
if(x==-1&&n%2==0) return 1;
if(x==-1&&n%2==1) return -1;
if(n==-2147483648) return 1.0/(x*myPow(x,2147483647));
if(n<0) return 1.0/myPow(x,0-n);
if(n%2==0) return myPow(x*x,n/2);
else return myPow(x*x,n/2)*x;
}
};
【一天一道LeetCode】#50. Pow(x, n)的更多相关文章
- LeetCode 50. Pow(x, n) 12
50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...
- LeetCode - 50. Pow(x, n)
50. Pow(x, n) Problem's Link ----------------------------------------------------------------------- ...
- leetcode 50. Pow(x, n) 、372. Super Pow
50. Pow(x, n) 372. Super Pow https://www.cnblogs.com/grandyang/p/5651982.html https://www.jianshu.co ...
- Java实现 LeetCode 50 Pow(x,n)
50. Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...
- [LeetCode] 50. Pow(x, n) 求x的n次方
Implement pow(x, n), which calculates x raised to the power n(xn). Example 1: Input: 2.00000, 10 Out ...
- LeetCode 50 Pow(x, n) (实现幂运算)
题目链接:https://leetcode.com/problems/powx-n/?tab=Description Problem:实现幂运算即 pow(x,n) 设形式为pow(x,n) ...
- LeetCode 50 - Pow(x, n) - [快速幂]
实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10输出: 1024.00000 示例 2: 输入: 2.10000, 3输出: 9.26100 示例 ...
- [leetcode]50. Pow(x, n)求幂
Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, 10 Ou ...
- Leetcode——50.Pow(x, n)
@author: ZZQ @software: PyCharm @file: leetcode50_myPow.py @time: 2018/11/22 13:58 要求:实现 pow(x, n) , ...
- Leetcode 50.Pow(x,n) By Python
实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, 3 输出: 9.26100 ...
随机推荐
- Docker配置 DNS
Docker 没有为每个容器专门定制镜像,那么怎么自定义配置容器的主机名和 DNS 配置呢? 秘诀就是它利用虚拟文件来挂载到来容器的 3 个相关配置文件. 在容器中使用 mount 命令可以看到挂载信 ...
- mybatis常用配置
前面两篇博客我们简单介绍了mybatis的使用,但是在mybatis的配置问题上我们只是使用了最基础的配置,本文我们就来说说其他一些常用的配置.如果小伙伴对mybatis尚不了解,可以先参考这两篇博客 ...
- Android反编译(未混淆的apk)
Android反编译(未混淆的apk) 工具 dex2jar 下载地址:我的CSDN 或者 官网 jd-gui 下载地址:我的CSDN 或者 官网 反编译步骤 1. 将APK解压缩,获取classes ...
- Ubuntu安装telent服务器时出现:apt-get:Package has no installation
当我在终端敲下这条命令的时候,系统就提示telnetd:apt-get:Package has no installation sudo apt-get install xinetd telnetd ...
- Activity平移动画
Activity平移动画 效果图 添加动画文件 在res下添加anim文件夹,在anim下添加几个动画文件,分别是进入和退出的动画时间和移动距离,属性很简单,一看就懂,不磨叽了. tran_next_ ...
- Scikit-learn:scikit-learn快速教程及实例
http://blog.csdn.net/pipisorry/article/details/52251305 :7] y = dataset[:,8] 我们将在下面所有的例子里使用这个数据组,换言之 ...
- mysql进阶(二十八)MySQL GRANT REVOKE用法
mysql进阶(二十八)MySQL GRANT REVOKE用法 MySQL的权限系统围绕着两个概念: 认证->确定用户是否允许连接数据库服务器: 授权->确定用户是否拥有足够的权限执 ...
- Linux telnet远程登录操作
telnet (如果不行 可以却换root帐户试试 su - root) 1.安装telnet-server sudo dpkg -i xinetd_1%3a2.3.14-7ubuntu3_ ...
- Spring MVC Junit4 单元测试 JunitTest
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/config/spring3/ap ...
- Erlang 集群互连测试
Erlang 集群互连测试Erlang节点相同cookie全互联成为一个集群(cluster).如果2个集群不同cookie, 然后其中有节点连接到对方集群的节点,这2个集群会合并成一个集群吗?连接到 ...