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 ...
随机推荐
- PyCharm 快捷键总结
运行 Shift+alt+F10 执行程序 调试 Shift+alt+F9 Debug调试 Shift + F9 对当前文件进行Debug F8 调试模式下 跳过 F7 调试模式下 进入 F9 快速调 ...
- Alfred添加百度搜索
Alfred默认的搜索只有 google; amazon和wikipakia, 我想加个百度搜索,怎么添加呢? 1.首先添加百度搜索,添加http://www.baidu.com/s?wd={quer ...
- 只用一次循环开销 将类似 1 A 、1 B 的数据返回成为 1 A,B 的拼接形式
/// <summary> ///将类似 1 A .1 B 的数据返回成为 1 A,B 的拼接形式 /// </summary> /// <param name=&quo ...
- 交换机的MAC地址作用
交换机的MAC地址在交换机进行数据交换时是没有作用的,因为交换机并不对转发的数据帧进行拆包重封装. 如果只是完成数据帧交换,则可以不要MAC地址(仅指二层交换机,三层交换机完成路由功能自然每个端口得有 ...
- 对CSDN的理性吐槽
CSDN博客网站首页挂了....从使用CSDN博客以来,大大小小的故障出过十几次.........再这样的话我都要对这个网站失去信心了
- MariaDB半同步复制
1.主从复制原理 MySQL的二进制日志(binglog)会记录所有对数据库进行更改的操作,也就是说只要是会对数据库产生修改的操作都会被记录到二进制日志中去.记录二进制日志的主要目的有两方面:a.恢复 ...
- Aizu 2300 Calender Colors dfs
原题链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2300 题意: 给你一个图,让你生成一个完全子图.使得这个子图中每个点的最 ...
- getServletConfig().getInitParameter("count1") java.lang.NullPointerException
通常在doget中 System.out.println(getServletConfig()); System.out.println(getServletConfig().getInitParam ...
- gradients的一些注意点
Each variable has a [.grad_fn] attribute that references a Function that has created the Variable(ex ...
- 连接Zookeeper操作
public class ZKConnector implements Watcher{ private static final Logger logger =LoggerFactory.getLo ...