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 ...
随机推荐
- centos 7 安装golang1.12.5
本文主要介绍服务器端环境配置,开发环境是window的话可以参考 https://www.cnblogs.com/nickchou/p/10765743.html 方式一.用yum安装 1.用yum指 ...
- DB2数据库报 [SQL0805N Package "NULLID.SQLLD003" was not found.]
解决办法: cd /home/db2inst1/sqllib/bnddb2 bind @db2cli.lst blocking all grant public sqlerror continue C ...
- python对象的复制问题,按值传递?引用传递?
这部分这篇博文说的很明白,转了过来 作者:winterTTr (转载请注明)http://blog.csdn.net/winterttr/article/details/2590741#0-tsina ...
- 分布式定时任务调度系统技术解决方案(xxl-job、Elastic-job、Saturn)
1.业务场景 保险人管系统每月工资结算,平安有150万代理人,如何快速的进行工资结算(数据运算型) 保险短信开门红/电商双十一 1000w+短信发送(短时汇聚型) 工作中业务场景非常多,所涉及到的场景 ...
- iOS 动画笔记 (一)
你也肯定喜欢炫酷的动画! 在APP中,动画就是一个点睛之笔!可以给用户增加一些独特的体验感,估计也有许多的和我一样的,看着那些觉得不错的动画,也就只能流口水的孩子,毕竟可能不知道从哪里下手去写!动画学 ...
- Linux笔记:vim
文件搜索后显示高亮,即使退出编辑高亮依然存在.使用以下几个方法: 1)指令模式下运行:nohlsearch 2)运行set nohlsearc,可永久关闭搜索高亮 3)搜索任意不存在的字符串
- 基于VUE开发项目
前言 最近由于公司需要,需要写一个相对来说比较大型的后台管理系统.为了保证管理系统操作体验较为舒适并且项目后期益于维护,最后决定基于VUE全家桶来开发一个高度组件化的单页SPA应用. 技术选型 vue ...
- python 设计模式之MVC模式
一.简单介绍 mvc模式 the model-view-controller pattern mvc模式是一个运用在软件工程中的设计模式.mvc模式脱离了以前简单的web服务设计逻辑,将开发,测试 ...
- C++并发实战 与多线程
http://blog.csdn.net/column/details/ccia.html
- C# 导出 数据 到Excel
/// <summary> /// 实现将数据导出至Excel, /// 在上面的代码中,我们首先将gridview绑定到指定的数据源中,然后在button1的按钮(用来做导出到EXCEL ...