[leetcode.com]算法题目 - Pow(x, n)
Implement pow(x, n).
class Solution {
public:
double pow(double x, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (==n) return 1.0;
if (==n) return x;
int k = abs(n);
int remainder = k % ;
double result = ;
result = pow(x, k/);
result = result*result*(==remainder?x:);
if (n>)
return result;
else
return 1.0/result;
}
};
我的答案
思路:使用递归,思路会比较清晰。注意讨论n小于0的情况。
[leetcode.com]算法题目 - Pow(x, n)的更多相关文章
- [leetcode.com]算法题目 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode.com]算法题目 - Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode.com]算法题目 - Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode.com]算法题目 - Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [leetcode.com]算法题目 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [leetcode.com]算法题目 - Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [leetcode.com]算法题目 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode.com]算法题目 - Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. class Solution { public: int sqr ...
- [leetcode.com]算法题目 - Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- 跟我学Spring Boot(二)Hello World
1.打开DemoApplication添加如下代码 package com.example; import org.springframework.boot.SpringApplication; im ...
- 【Redis】安装及简单使用
Redis介绍 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化 ...
- Java crash问题分析
Java的应用有时候会因为各种原因Crash,这时候会产生一个类似java_errorpid.log的错误日志.可以拿到了 这个日志,怎样分析Crash的原因呢?下面我们来详细讨论如何分析java_e ...
- RHEL6.3下挂载ISO并配置安装软件包(转)
1.将RHEL6.3的ISO镜像上传至RHEL6.3服务器上 2.挂载ISO镜像 一般将镜像文件挂载到/mnt/XXX下,所以首先创建挂载文件夹: # mkdir /mnt/cdrom 挂载(我将上传 ...
- oracle 替换字符 replace
替换字符: select replace('xxyyzz','zz','') from dual >> xxyy 同时替换多个字符: select replace(replace('x ...
- 系统当前时间system.currenttimemillis与new Date().getTime() 区别
system.currenttimemillis //取到毫秒数,并且执行效率高 new Date().getTime()没他精确
- zk可视化工具
也是无意中接触到zk,搞得有点头大,之前都是通过crt连通服务后,在服务上通过命令去查看节点下的数据的,十分的不方便,后来发现了可视化工具这玩意儿还真的是好用,看节啊点下的数据啥的一目了然,我用过的有 ...
- 关于DOM级别的一些问题,DOM0,DOM1,DOM2
之前看书没太注意这个问题,直到我今天看书看到一个DOM0级,于是我就在群里问了下各个级别的意思区别.. 首先我们的确定标准了是没有DOM0级的.在平时阅读的时候可能会读到DOM0级(DOM Level ...
- 强大的DataGrid组件[1]
说明:DataGrid组件是Silverlight数据组件中最为常用并且是功能最为强大的数据组件.因此,对开发者而言,深入了解其特性是十分有必要的.本文先介绍该组件的基本特性,接着通过几个简单实例来说 ...
- MFC载入JPG图片
## 1.定义画图函数 HRESULT CIPCamDlg::draw(char *lpImageFile, HWND hWnd, int nScrWidth, int nScrHeight) { H ...