leetcode笔记:Pow(x, n)
一. 题目描写叙述
Implement pow(x, n).
二. 题目分析
实现pow(x, n)。即求x的n次幂。
最easy想到的方法就是用递归直接求n个x的乘积,这里须要依据n的值,推断结果是正数还是负数,这样的方法的时间复杂度为O(n)。
更加快捷的方法是。使用分治法。对于x^n。有一下公式:
x^n = x^(n / 2) * x^(n / 2) * x^(n % 2)
使用这样的方法的时间复杂度为O(logn)。
三. 演示样例代码
#include <iostream>
using namespace std;
class Solution {
public:
double pow(double x, int n)
{
if (n == 0)
return 1;
if (n > 0)
return power(x, n);
else
return 1 / power(x, -1 * n);
}
private:
double power(double x, int n)
{
if (n == 0)
return 1;
double a = power(x, n / 2); // 递归求x^(n/2)
if (n % 2 == 0)
return a * a;
else
return a * a * x;
}
};
四. 小结
此题为分治思路的经典题型之中的一个。
leetcode笔记:Pow(x, n)的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- Leetcode 笔记 35 - Valid Soduko
题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
随机推荐
- Scrapy学习-22-扩展开发
开发scrapy扩展 定义 扩展框架提供一个机制,使得你能将自定义功能绑定到Scrapy. 扩展只是正常的类,它们在Scrapy启动时被实例化.初始化 注意 实际上自定义扩展和spider中间件. ...
- java 修改类注释
在Windows->Preferences->Java->Code Style->Code Templates 的 Comments中 Types 是控制类的注释 /** * ...
- Rust-HayStack
src/main.rs extern crate multipart; extern crate iron; extern crate time; //image converter extern c ...
- HDU 1020 Encoding【连续的计数器重置】
Encoding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17171 Accepted: 11999 Descr ...
- mysql数据库基本操作(六)
外键约束 创建外键 前面讲的表单查询都是一张表,但项目中表与表之间是有关联的,比如我们创建的学生表,他们可能在不同班级,不同班级有不同的班主任,他们之间的关系大概是这样的:每一个班主任会对应多个学生 ...
- 列表的 sort
题目:输入三个整数x,y,z,请把这三个数由小到大输出. 实例 #!/usr/bin/python # -*- coding: UTF-8 -*- l = [] for i in range(3): ...
- php设置报错级别
ini_set("display_errors", "On");//若页面不报错的话,请设置php.ini 的display_errors 为 On error ...
- PostgreSQL 10.0 preview 功能增强
https://yq.aliyun.com/users/1384833841157402?spm=5176.100239.blogrightarea51131.3.yI7e9d
- Protel中的快捷键使用(网上资源)
使用快捷键之前,将输入法切换至中文(中国)状态 Enter——选取或启动 Esc——放弃或取消 F1——启动在线帮助窗 Tab——启动浮动图件的属性窗口 Page Up——放大窗口显示比例 Page ...