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. Imageview 按比例适应屏幕大小

    DisplayMetrics dm = new DisplayMetrics();//取得窗口属性getWindowManager().getDefaultDisplay().getMetrics(d ...

  2. php与html代码的若干转换

    以前懵懵懂懂的看过,没怎么在意,现在总结一下 一般来说,像留言板之类的content,用这样的就够了: $content=addslashes(htmlspecialchars($_POST['con ...

  3. 使用crf++

    在example文件夹下存在4个使用crf的实例 1.在命令行执行 进入路径:./example/seg 执行:sh exec.sh 2. 在python中执行 进入路径:./python 执行:(1 ...

  4. 【转】CentOS 7 安装配置 NFS

    环境 nps 192.168.1.97 client 192.168.1.98 一.yum 安装 yum -y install nfs-utils rpcbind nfs 的配置文件 /etc/exp ...

  5. 2018.11.04 洛谷P1081 开车旅行(倍增)

    传送门 思路简单码量超凡? 感觉看完题大家应该都知道是倍增sbsbsb题了吧. 首先预处理出从每个点出发如果是AAA走到哪个点,如果是BBB走到哪个点. 然后利用刚刚预处理出的信息再预处理从每个点出发 ...

  6. C++ char, unsigned char, signed char

    C语言中的 char, unsigned char, signed char 一.他们是什么? signed char是有符号的,但是unsigned char没有符号,两者在存储上没有任何区别都是8 ...

  7. attachEvent方法绑定事件

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. 在eclipse上搭建Roku开发环境

    环境:Oracle VM virtualBox+Ubuntu server 12.0.4.2 LTS+xfce+ Eclipse IDE for C/C++ Developers 4.3.2 参考:h ...

  9. linux ps查进程 kill关闭进程

    原文链接:http://blog.sina.com.cn/s/blog_53855ace0100ded4.html 首先,我们需要使用linux下另外一个ps命令查找与进程相关的PID号:ps aux ...

  10. C++获取当前进程绝对路径

    获取进程的绝对路径(代码同时操作字符串获取了文件目录): 第一种代码: wstring GetProgramDir() { TCHAR exeFullPath[MAX_PATH]; // Full p ...