【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 ...
随机推荐
- 15 个有用的 MySQL/MariaDB 性能调整和优化技巧(转载的一篇好文)
MySQL 是一个强大的开源关系数据库管理系统(简称 RDBMS).它发布于 1995 年(20年前).它采用结构化查询语言(SQL),这可能是数据库内容管理中最流行的选择.最新的 MySQL 版本是 ...
- Reactjs 入门基础(二)
如果我们需要向组件传递参数,可以使用 this.props 对象,实例如下: <body> <div id="example"></div> & ...
- C# byte数组转换成List<String>
byte[] bys=buffer; string[] AllDataList= Encoding.Default.GetString(bys).Split(Environment.NewLine. ...
- Hibernate的配置文件以及用法
一. 三大框架 Hibernate 1.安装hibernate插件至ecilpse 2.进行配置 2.1 主配置文件 <?xml version="1.0" encoding ...
- 样式link属性media用法--媒体类型查询
引用外部样式使用link 你可能想针对将要显示页面的设备类型(桌面PC.笔记本电脑.平板电脑.手机或者甚至页面的印刷版本)来调整页面的样式,可以利用一个media属性, 在<link>元素 ...
- angularJS学习之旅(1)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- [转]libsvm 训练后的模型参数讲解
http://blog.sina.com.cn/s/blog_6646924501018fqc.html 主要就是讲解利用libsvm-mat工具箱建立分类(回归模型)后,得到的模型model里面参数 ...
- 【MVC】 非常简单的页面导出 WORD, EXCEL方法
[MVC] 页面导出 WORD, EXCEL 前端 js function output() { var para = new Object(); para.html = getHtml(" ...
- Servlet 添加购物车
import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.It ...
- Spring----->projects----->Spring Web Flow
1.概述(about Spring Web Folw) Spring Web Flow是spring社区一个子project Spring Web Flow builds on Spring MVC ...