Implement pow(xn).

幂运算,简单的方法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) (幂运算)的更多相关文章

  1. LeetCode 50 Pow(x, n) (实现幂运算)

    题目链接:https://leetcode.com/problems/powx-n/?tab=Description   Problem:实现幂运算即 pow(x,n)   设形式为pow(x,n)  ...

  2. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  3. 求幂运算、多项式乘法及Horner法则的应用

    一,两种不同的求幂运算 求解x^n(x 的 n 次方) ①使用递归,代码如下: private static long pow(int x, int n){ if(n == 0) return 1; ...

  4. [ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)

    Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael ...

  5. POJ1026 Cipher(置换的幂运算)

    链接:http://poj.org/problem?id=1026 Cipher Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  6. 组合数学 - 置换群的幂运算 --- poj CARDS (洗牌机)

    CARDS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1448   Accepted: 773 Description ...

  7. 迭代加深搜索 codevs 2541 幂运算

    codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

  8. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  9. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

随机推荐

  1. 006-HotSpot JVM收集器

    一.概述 1.1.图解 上面有7中收集器,分为两块,上面为新生代收集器,下面是老年代收集器.如果两个收集器之间存在连线,就说明它们可以搭配使用. JVM给出了3类选择:串行收集器.并行收集器.并发收集 ...

  2. 【Sql Server】—sql Servler登录失败

    登录失败报错信息如下: 标题: 连接到服务器 ------------------------------ 无法连接到 localhost. ----------------------------- ...

  3. java 多线程 day03 线程同步

    package com.czbk.thread; /** * Created by chengtao on 17/12/3. 线程安全问题: 线程安全出现 的根本原因: 1. 存在两个或者两个以上 的 ...

  4. Appium中的logger

    原文地址http://blog.csdn.net/itfootball/article/details/45395901 appium中的log输出量很大,我们分析问题的时候会依赖于这些log,但是你 ...

  5. change the color of a disabled TEdit?

    change the color of a disabled TEdit? Author: P. Below Category: VCL {Question:How can I change the  ...

  6. k8s使用ceph作为后端存储挂载

    一.在ceph集群上操作: 1.创建池(主要使用存储类来进行持久卷的挂载,其他的挂载方式不好使也太麻烦):ceph osd pool create k8s 64 二.在k8s上操作: 1.安装客户端( ...

  7. C++白盒测试最佳实践课程,3个免费名额火热申请中,31号前截止申请...

    C++白盒测试最佳实践课程,3个免费名额火热申请中,31号前截止申请...http://automationqa.com/forum.php?mod=viewthread&tid=2561&a ...

  8. Libev和LibEvent

    libev和libevent功能基本相同,名称相近,到底该用哪一个呢?zhouhh@zhh64:~$ sudo apt-cache search libeventlibevent-dev – Deve ...

  9. sublime Text emmet插件使用手册

    转自:http://www.w3cplus.com/tools/emmet-cheat-sheet.html 介绍 Emmet (前身为 Zen Coding) 是一个能大幅度提高前端开发效率的一个工 ...

  10. js 科学计数法

    function convertNum(num_str){ //参数必须为 字符串 //科学计数法字符 转换 为数字字符, 突破正数21位和负数7位的Number自动转换 // 兼容 小数点左边有多位 ...