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 ...
随机推荐
- C++学习(二):学会使用stringstream
1.前言 今天在CppTemplateTutorial群里,有人问了一个问题:这一堆add怎么简化掉 https://wandbox.org/permlink/vDPDwMFbBIQSSymS.代码如 ...
- C#Json转Xml格式数据的方法
第一种方法 string Xml = "在这里写Json字符串"; XmlDictionaryReader reader = JsonReaderWriterFactory.Cre ...
- HDU 5997 rausen loves cakes(启发式合并 + 树状数组统计答案)
题目链接 rausen loves cakes 题意 给出一个序列和若干次修改和查询.修改为把序列中所有颜色为$x$的修改为$y$, 查询为询问当前$[x, y]$对应的区间中有多少连续颜色段. ...
- Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- POJ 1054 The Troublesome Frog 枚举
这个题分类是dp,想了一会没有想出来,就去看别人题解了.发现别人题解全是暴力枚举= =.复杂度超过 N^2,但可能是剪枝的作用,没有超时. 思路:将所有点按坐标由小到大排序.两两枚举点p1,p2,并判 ...
- luogu P3800 Power收集
题目背景 据说在红雾异变时,博丽灵梦单身前往红魔馆,用十分强硬的手段将事件解决了. 然而当时灵梦在Power达到MAX之前,不具有“上线收点”的能力,所以她想要知道她能收集多少P点,然而这个问题她答不 ...
- 利用Jdk 6260652 Bug解析Arrays.asList
在java.util.ArrayList源码中: c.toArray might (incorrectly) not return Object[] (see 6260652) 产生疑惑: 附上Jav ...
- 使用aspnet_regsql.exe 创建ASPState数据库,用来保存session会话
使用aspnet_regsql.exe 创建ASPState数据库,用来保存session会话 因为公司有多台服务器,所以session要保存在sql server上,因此要在数据库中建立存放se ...
- iOS -- iOS11新特性,如何适配iOS11
前言 这几天抽空把WWDC的Session看了一些,总结了一些iOS11新的特性,可能对我们的App有影响,需要我们进行适配.本文作为一个总结. 本文内容包括:集成了搜索的大标题栏.横向选项卡栏.Ma ...
- hdu1862
//开始把student stu[100000]放置在main()中导致栈溢出,所以必须放在全局位置, //可以调用数组的排序函数sort,包含头文件#include<algorithm> ...