leetcode — powx-n
/**
* Source : https://oj.leetcode.com/problems/powx-n/
*
* Created by lverpeng on 2017/7/18.
*
* Implement pow(x, n).
*
*/
public class Pow {
/**
* 实现x^n
* 考虑n为负数
* 当n为偶数的时候,计算x*x,以期让n快速收缩
*
* @param x
* @param n
* @return
*/
public double pow (int x, int n) {
boolean sign = true; // true:正数
if (n < 0) {
n = -n;
sign = false;
}
double result = 1.0;
while (n > 0) {
if ((n & 1) == 1) {
result *= x;
}
n = n >> 1;
x *= x;
}
return sign ? result : 1.0 / result;
}
public static void main(String[] args) {
Pow pow = new Pow();
System.out.println(pow.pow(2, 3));
System.out.println(pow.pow(2, 10));
System.out.println(pow.pow(2, -3));
}
}
leetcode — powx-n的更多相关文章
- [Leetcode] powx n x的n次方
Implement pow(x, n). 题意:计算x的次方 思路:这题的思路和sqrt的类似,向二分靠近.例如求4^5,我们可以这样求:res=4.4*4^4.就是将每次在res的基础上乘以x本身, ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- 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 ...
随机推荐
- 2019.03.12 codeforces739E. Gosha is hunting(dp凸优化)
传送门 题意:nnn个物品,有aaa个XXX道具和bbb个YYY道具,XXX道具移走第iii个物品概率为pip_ipi,YYY道具移走第iii个道具概率为uiu_iui. 对于每个物品每种道具最多 ...
- Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂
https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...
- 1003 Emergency Dijkstra
这题做的心很累,我用的还是 1018的思路做的,但是 使用dfs 求最大人数对于某些有问题(现在也不知道错哪了), 看了别人的代码后才发现,其实完全不用这么麻烦,只需设置一个点的权重,一遍DJ(自创简 ...
- shell搭建CentOS_7基础环境
#!/bin/bash#Auth:Darius#CentOS_7配置实验环境eno=`ifconfig|awk '{print $1}'|head -1|awk -F ":" '{ ...
- mpvue 初体验之改写车标速查小程序
前文 说到我开发了一个简单的小程序叫做 车标速查(代码以及二维码详见 这里),本文简单讲讲如何将这个小程序转为 mpvue 开发(最终 成果 ) mpvue 官网的 文档 真的是非常简单,不,应该说是 ...
- What's the Difference Between Iterators and Generators in Python
https://www.quora.com/Whats-the-difference-between-iterators-and-generators-in-Python
- VS Code 之 smarty 扩展
VS Code 中的 Smarty 扩展: https://github.com/imperez/vscode-smarty 目前(v0.2.0)不支持定制定界符.可以通过 trick 的方式篡改. ...
- git的基本使用和问题
1,填写信息git config --global user.name "用户名"git config --global user.email "邮箱" 2,创 ...
- PYTHON进阶(3)
学习内容: 1.Python模块redis 2.Python模块memcach 3.Python模块SQLAlchemy 一.Python模块redis redis介绍 二.Python模块memca ...
- tornadoの2
tornado之日记本—— 1.diary.py: import time from model.database import DataBase from tornado import we ...