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. ros pluginlib 段错误

    最近在重新回看ROS插件时,运行出现了段错误,发现是boost版本问题,我目前版本是1.66,应该调整至1.58版本,如果跟其他软件使用不同的boost版本时,可以把相应版本编译到本地,不instal ...

  2. .NET获取城市信息(将三字代码转换成城市名)

    整理代码,发现有一个从两张表里读取城市列表,然后linq和lambda表达式来获取城市名的函数,代码如下: public static string GetCityHotelText(string c ...

  3. Visual Studio Plus 开发

    参考文档:Developing Visual Studio Extensions http://msdn.microsoft.com/en-us/library/dd885119(v=vs.120). ...

  4. PID控制算法的C语音实现

    http://wenku.baidu.com/link?url=_u7LmA1-gzG5H8DzFYsrbttaLdvhlHVn5L54pgxgUiyyJK_eWtX0LbS7d0SEbHtHzAoK ...

  5. 回文日期(NOIP2016)

    题目:回文日期 这题虽然说不难,但是也不能算水了. 我先讲讲思路.60分的算法很好写,就是判断一下是不是回文串,分离每个数位,判断即可. 但我们的目标是满分,所以我来讲讲满分算法. 首先,给的是区间, ...

  6. 协程的NullReferenceException 错误

    public void loadPic(string url) { WWW www = new WWW(url); StartCoroutine(WaitForRequest(www)); } IEn ...

  7. 数据分析报告格式zz

    分析报告的输出是是你整个分析过程的成果,是评定一个产品.一个运营事件的定性结论,很可能是产品决策的参考依据,既然这么重要那当然要写好它了. 我认为一份好的分析报告,有以下一些要点: 首先,要有一个好的 ...

  8. 812. Largest Triangle Area

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  9. hdu-1059(多重背包+二进制优化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意:输入6个数,每个数ni代表价值为i的物品有ni个.求如果这些物品能均分给两个人,每个人获得 ...

  10. springboot mybatis 分页整合

    spring boot 整合mybatis ,分两块mybatis 整合,分页整合.   1.pom文件增加 <dependency> <groupId>org.mybatis ...