【Leetcode】【Medium】Pow(x, n)
Implement pow(x, n).
解题思路:
求浮点数的幂次方,注意可能为负数次幂;
可以使用二分搜索的思想,当n为偶数时,x^n = x^(n/2) * x^(n/2),因此只需要求得一半的幂次方,将结果平方,就得到所求结果。
解题步骤:
1、递归最底层,n == 0 时,返回1;
2、求出t = pow(x, n/2);
3、判断n的奇偶性:
a. 如果奇数,要判断n的符号,n为负数乘以1/x,n为正数时乘以x;
b. 如果偶数,不需要多乘;同时,也不需要判断符号,因为符号形式已经包含在待乘的两个t中;
代码:
class Solution {
public:
double myPow(double x, int n) {
if (n == )
return ;
double t = myPow(x, n / );
if (n % ) {
return n < ? /x*t*t : x*t*t;
} else {
return t*t;
}
}
};
【Leetcode】【Medium】Pow(x, n)的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode每天一题】Pow(x, n)(平方)
Implement pow(x, n), which calculates x raised to the power n (x,n). Example 1: Inpu ...
- 【leetcode刷题笔记】Pow(x, n)
Implement pow(x, n). 题解:注意两点: 普通的递归把n降为n-1会超时,要用二分的方法,每次把xn = x[n/2] * x[n/2] * xn-[n/2]*2, [n/2]表示n ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
随机推荐
- 如何防止ElasticSearch集群出现脑裂现象(转)
原文:http://xingxiudong.com/2015/01/05/resolve-elasticsearch-split-brain/ 什么是“脑裂”现象? 由于某些节点的失效,部分节点的网络 ...
- json2.js源码解读记录
相关内容:json详细用法.js语法.unicode.正则 json特点--最简单.最小巧的经典js库. json作者:道克拉斯.克劳福德(Douglas Crockford)--js大牛 出 ...
- pcm跟.wav文件的关系
PCM(Pulse Code Modulation----脉码调制录音).所谓PCM录音就是将声音等模拟信号变成符号化的脉冲列,再予以记录.PCM信号是由[1].[0]等符号构成的数字信号,而未经过任 ...
- 深入理解js——原型的灵活性
在java中,class是一个模子,对象就是按照这个模子刻出来的:但是在JavaScript中对象可以刻成任意的样子. 首先,对象属性可以随时改动.对象或者函数,刚开始new出来之后,可能啥属性都没有 ...
- spi_flash
http://blog.chinaunix.net/uid-27406766-id-3384699.html
- asp.net GDI+绘制折线
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- node中的可读流和可写流
javascript的一个不足之处是不能处理二进制数据,于是node中引入了Buffer类型.这个类型以一个字节(即8位)为单位,给数据分配存储空间.它的使用类似于Array,但是与Array又有不同 ...
- Visio控件关闭“形状”面板
Visio.Window winShapeSearch = axDrawingControl1.Window.Windows.get_ItemFromID((int)Visio.VisWinTypes ...
- Metadata file 'xxx.dll' could not be found 已解决
最近学习三层架构,在网上找了个权限管理的源码研究,发现编译不通过,到处都是Metadata file 'xxx.dll' could not be found,找了两天原因都没找到答案. 然后试着去编 ...
- vi 文字处理器
vi 的使用 基本上 vi 共分为三种模式,分别是『一般模式』. 『编辑模式』与『指令列命令模式』三种! 这三种模式的作用是: 一般模式: 以 vi 处理一个档案的时后,一进入该档案就是一般模式了.在 ...