【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 ...
随机推荐
- 【转载】C++ function、bind和lambda表达式
本篇随笔为转载,原贴地址:C++ function.bind和lambda表达式. 本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lambda表达式, function对象和bind机制 ...
- WPF的图片操作效果(一):RenderTransform
一.RenderTransform类的成员: 1.TranslateTransform 平移效果 2.RotateTransform 旋转效果 3.ScaleTransform 缩放效果 ...
- 关于CacheLookup一个有趣的问题
今天写一个与其他系统进行物料同步的接口,通过COM Business Connector调用Axapta3.0的方法将数据插入到物料表中,中间发生异常,事务回滚,再次调用的时候提示刚刚发生异常的物料已 ...
- codeforces 361 E - Mike and Geometry Problem
原题: Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him ...
- SQL常用代码收集
1.存储过程中,使用in查询时的参数处理方式 使用情形描述:传入存储过程的参数为一个字符串@IDs,以固定分隔符连接 新建字符串分割函数,然后将分割结果传入存储过程: CREATE FUNCTION ...
- VC MFC在CMFCToolBar工具栏中加入组合框
如何在CMFCToolBar工具栏中加入组合框等控件,且先看在线MSDN上怎么说的: 要增加一个组合框,需要完成以下步骤: 1.在工具栏资源中,增加一个对应ID资源号的按钮. 2.在主框架(mainf ...
- JQuery this 和 $(this) 详解
this this 在面向对象语言中,指代调用上下文对象,在js中,也是一模一样的概念,所以不管这个this出现在什么地方,只要追踪到当前调用对象是什么,那么this是什么也就一目了然了. 先看一个 ...
- NPOI 读写Excel
实例功能概述: 1.支持Excel2003以及2007 2.支持Excel读取到DataTable(TableToExcel) 3.支持DataTable导出到Excel(TableToExcel) ...
- js浮点数计算问题 + 金额大写转换
一 js浮点数计算问题解决方案: 1.使用 NumberObject.toFixed(num) 方法 toFixed() 方法可把 Number 四舍五入为指定小数位数的数字. 2.较精度计算浮点数 ...
- C# 命令绑定
在构建WinForm程序中,为了使系统的结构清晰,有好的用户交互体验,实现不同按钮之间的交互,不使主窗体里面的代码臃肿.将按钮的命令通过类进行绑定,实现命令的管理使很有必要的. 该文章是将如何实现Bu ...