Pow(x, n)

Implement pow(xn).

计算x的n次方。

解题思路:

考虑到n的值会很大,而且可为正可为负可为0,所以掉渣天的方法就是用递归了。

对了,这题也在《剑指offer》里面有提到,是面试题11:数值的整数次方。可以书里给的递归代码在n为负数的情况下是错误的……他没有考虑到n为奇数时,n是正数和n是负数的情况是不一样的。

具体参考这哥们的答案。

这里我摘抄下来,方便以后查询。

double pow(double x, int n) {
if (n == ) return 1.0;
// Compute x^{n/2} and store the result into a temporary
// variable to avoid unnecessary computing
double half = pow(x, n / );
if (n % == )
return half * half;
else if (n > )
return half * half * x;
else
return half * half / x;
}

【LeetCode练习题】Pow(x, n)的更多相关文章

  1. 【LeetCode练习题】Permutation Sequence

    Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...

  2. Leetcode练习题Remove Element

    Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...

  3. [LeetCode] Super Pow 超级次方

    Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large posi ...

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

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

  5. [LeetCode 题解]: pow(x,n)

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Implement po ...

  6. [LeetCode] 50. Pow(x, n) 求x的n次方

    Implement pow(x, n), which calculates x raised to the power n(xn). Example 1: Input: 2.00000, 10 Out ...

  7. LeetCode 50. Pow(x, n) 12

    50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...

  8. leetcode Super Pow

    题目描述: superPow(int a, int[] b),b是一个int数组,每个元素都是正的个位数,组合起来表示一个正整数,例如b=[1,2,3]表示123,求解a^b mod 1337. 思路 ...

  9. 【leetcode】Pow(x,n)

    马上各种校招要开始了,怎么也得准备一下,之前一直在看看机器学习,NLP方面的东西,收获很多.最近换换脑子,回过头来做做leetcode,感觉还是蛮有意思的.今天刷了个水题,AC不高,然而难度也不高.. ...

随机推荐

  1. Search for a Range 解答

    Question Given a sorted array of integers, find the starting and ending position of a given target v ...

  2. OS快速开发必备

    github:https://github.com/koknine (终于改成以前的了) 当前移动互联网行业太火爆,移动端的需求日益增长,很多开发人员每天都应对着各种需求,作为一名iOS开发人员,对于 ...

  3. 迭代 Iterate

    迭代:指按照某种顺序逐个访问列表中的每一项.比如:for语句 逐个访问: lst = ['q', 'i', 'w', 's', 'i', 'r'] for i in lst: print (i), # ...

  4. 配置数据库连接池,Tomcat6.0 连接池的配置

    Tomcat6.0 连接池的配置1.本人当前使用的Tomcat版本为:6.0.20,oracle为稳定的9i版本 2.下文为方便起见,依习惯以%Tomcat_Home%表示Tomcat安装的目录,本人 ...

  5. js获取某个标签中的信息

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  6. linux磁盘管理、新增磁盘、分区、挂载

    1. du -sh 查看目录.文件总大小 -a:全部文件与目录大小都列出来.如果不加任何选项和参数只列出目录(包含子目录)大小. -c:最后加总2. df -h 查看磁盘使用量3. lsblk 查看系 ...

  7. web去掉浏览器自带默认样式

    @charset "utf-8"; ;;} body{font-size:12px;} img{border:none;} ul,ol{list-style:none;} inpu ...

  8. 《JavaScript 闯关记》之 BOM

    ECMAScript 是 JavaScript 的核心,但如果要在 Web 中使用 JavaScript,那么 BOM(浏览器对象模型)则无疑才是真正的核心.BOM 提供了很多对象,用于访问浏览器的功 ...

  9. System.Speech.Synthesis 添加暂停、继续功能

    为了方便调用暂停.继续的方法.要将speech的功能写成一个类.直接附上代码: using System; using System.Collections.Generic; using System ...

  10. Android 定义重名权限问题

    一直以来对android的权限机制就有一个疑问,因为在使用权限时,实际上只需要permission的name这一个标签,而在定义权限时,android是不会检查是否重名的,那么在两个应用定义了重名权限 ...