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. 【HDU1875】畅通工程再续(MST基础题)

    更改成实形数即可.第一次敲完直接交,CE了一次.晕. #include <iostream> #include <cstring> #include <cstdio> ...

  2. 表单javascript checkbox全选 反选 全不选

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

  3. pyqt QTreeWidget例子学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import  * from Py ...

  4. 关于safari上的select宽高问题小技,自定义下拉框

    之前一直用windows做开发,最近换了个mac,在几经折腾之下,安装完了各种开发工具,IDE等,然后欣然打开自己正在开发的网站.突然发现mac上所有的下拉框都变了,都是默认样式,无论padding, ...

  5. Sort(归并)

    Sort 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 You want to processe a sequence of n distinct integers ...

  6. 0..n去掉一个数,给你剩下的数,找出去掉的那个数

    转载请注明转自blog.csdn.net/souldak , 微博@evagle 首先,考虑没有去掉那些数,如果n是奇数,n+1个最低位肯定是0101...01,count(0)=count(1),如 ...

  7. Cts框架解析(8)-IBuildProvider

    IBuildProvider接口中定义了三个方法 /* * Copyright (C) 2010 The Android Open Source Project * * Licensed under ...

  8. windows 和linux 同步api对比

    初始化临界区 (win) InitializeCriticalSection(RTL_CRITICAL_SECTION &rtl_critial_section) (linux) pthrea ...

  9. 微信jssdk批量展示卡包中的卡券

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  10. SQL:define和verify命令及替换变量&

    =================替换变量&===============使用一个&符号来指定一个变量值,执行SQL语句时,会提示用户输入一个数值. SQL> select sa ...