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. HttpURLConnection 返回汉字乱码(全是问号)

    public static String doPost(String urlStr, Map<String, Object> paramMap) throws Exception { UR ...

  2. import this

    import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than ...

  3. 华为QOS原理及配置

    http://www.tudou.com/programs/view/GWCiHfWC9FI/ FLASH : http://www.tudou.com/v/GWCiHfWC9FI/&reso ...

  4. Java的GUI设计小技巧

    不可关闭窗口 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

  5. Bootstrap之javascript组件

    一 模态框 模态框放在body标签里面的,是body标签的子元素 静态实例: <div class="modal fade" tabindex="-1" ...

  6. 2019.01.20 bzoj3784: 树上的路径(二分答案+点分治)

    传送门 点分治好题. 题意简述:给一棵带边权的树,问所有路径中前mmm大的.m≤300000m\le300000m≤300000 思路: 网上有题解写了可以通过什么点分治序转化成超级钢琴那道题的做法蒟 ...

  7. 如何制作chm文件

    本文介绍如何从一个包中的docs文档生成一个chm文档. 1,准备软件Easy CHM 这个网上有下载,下载后安装,至于怎么使用,等下再介绍.安装之后如下图. 2,准备文件 比如我这里下载了一个cxf ...

  8. arguments[0]()的详解

    var length = 10; function fn(){ console.log(this.length); } var obj = { length:5, method:function(fn ...

  9. 安卓4.2用adb 获取屏幕分辨率等信息

    在终端输入adb shell dumpsys: 终端会打印出一些service list 用adb shell dumpsys+service名即可查询相应的信息. 屏幕分辨率用adb shell d ...

  10. shell脚本-成长之路

    我对shell脚本的认识,除了执行过同事写的shell 脚本外,其他一无所知,为了让自己强大,我决定自己研究shell脚本,也许在你看来很简答,没必要说这么多废话,但是我希望在我的技术log里记录下来 ...