Implement pow(xn).

 class Solution {
public:
double pow(double x, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (==n) return 1.0;
if (==n) return x; int k = abs(n);
int remainder = k % ; double result = ; result = pow(x, k/);
result = result*result*(==remainder?x:); if (n>)
return result;
else
return 1.0/result;
}
};

我的答案

思路:使用递归,思路会比较清晰。注意讨论n小于0的情况。

[leetcode.com]算法题目 - Pow(x, n)的更多相关文章

  1. [leetcode.com]算法题目 - Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  2. [leetcode.com]算法题目 - Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. [leetcode.com]算法题目 - Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  4. [leetcode.com]算法题目 - Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  5. [leetcode.com]算法题目 - Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  6. [leetcode.com]算法题目 - Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. [leetcode.com]算法题目 - Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  8. [leetcode.com]算法题目 - Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. class Solution { public: int sqr ...

  9. [leetcode.com]算法题目 - Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

随机推荐

  1. DevOps:从理念到实施

    转载自:http://os.51cto.com/art/201404/436794.htm DevOps这个新理念的出现,是为了应对IT环境中普遍面临的一些挑战.开发团队要求的不断满足新的客户需求,并 ...

  2. Devexpress VCL Build v2015 vol 15.1.2发布

    2015年马上过半年了.终于第一个大版出来了. What's New in 15.1.2 (VCL Product Line)   New Major Features in 15.1 What's ...

  3. Tomcat架构解析(二)-----Connector、Tomcat启动过程以及Server的创建过程

    Connector用于跟客户端建立连接,获取客户端的Socket,交由Container处理.需要解决的问题有监听.协议以及处理器映射等等. 一.Connector设计   Connector要实现的 ...

  4. 使用 docker compose 安装 tidb

    目标 : 单机上通过 Docker Compose 快速一键部署一套 TiDB 测试集群 前提条件: 1.centos版本在7.3 以上 2.安装git 3.安装docker Docker versi ...

  5. excel2007自定义菜单项学习

    参考: http://club.excelhome.net/thread-1288002-1-1.html http://club.excelhome.net/thread-709306-1-1.ht ...

  6. IntelliJ IDEA 2017版 编译器使用学习笔记(八) (图文详尽版);IDE快捷键使用;IDE代码重构(寻找修改痕迹)

    git集成: 快速找到版本控制器中某段代码的作者 一.annotate            选中某行代码,右键,选择annotate,鼠标放于其上就会显示注释    二.移动所有改动之处: prev ...

  7. 微信小程序的新的

    app.request.get('http://ele.kassing.cn/v1/pois',this.data.city).then(res=>{ console.log(res) this ...

  8. hadoop sqoop的常用名命令

    1 列出所有的ambari数据库中所有的表 Sqoop list-tables -connect  jdbc:mysql://localhost:3306/ambari -username ambar ...

  9. BAT文件语法和技巧(bat文件的编写及使用)

    源文链接:http://www.jb51.net/article/5828.htm 比较有用的东比较有用的东西 首先,批处理文件是一个文本文件,这个文件的每一行都是一条DOS命令(大部分时候就好象我们 ...

  10. 改Android手机定位位置

    手机定位方法 1,gps等卫星定位,如美国的gps,欧洲的伽利略,中国的北斗等,通过至少三颗卫星,用三角定位和时间等算法,计算出设备的经纬度,到地图上找到这个经纬度的地名 2,移动运营商基站定位,通过 ...