Java for LeetCode 029 Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
解题思路:
既然不呢个用乘除和取模运算,只好采用移位运算,可以通过设置一个length代表divisor向左的做大移位数,直到大于dividend,然后对length做一个循环递减,dividend如果大于divisor即进行减法运算,同时result加上对应的值,注意边界条件,JAVA实现如下:
static public int divide(int dividend, int divisor) {
if (divisor == 0)
return Integer.MAX_VALUE;
boolean isNeg = dividend < 0;
if (divisor < 0)
isNeg = !isNeg;
int length = 0;
long longdividend = Math.abs((long) dividend);
long longdivisor = Math.abs((long) divisor), result = 0;
while (longdividend >= longdivisor) {
longdivisor = longdivisor << 1;
length++;
}
while (length >= 0) {
if (longdividend >= longdivisor) {
result += Math.pow(2, length);
longdividend -= longdivisor;
}
longdivisor = longdivisor >> 1;
length--;
}
if (result > Integer.MAX_VALUE && !isNeg)
result = (long) Integer.MAX_VALUE;
return isNeg ? -(int) result : (int) result;
}
Java for LeetCode 029 Divide Two Integers的更多相关文章
- LeetCode 029 Divide Two Integers
题目要求:Divide Two Integers Divide two integers without using multiplication, division and mod operator ...
- Java [leetcode 29]Divide Two Integers
题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...
- 【LeetCode】029. Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- 【leetcode】Divide Two Integers (middle)☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode] 29. Divide Two Integers ☆☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- 【CodeForces 504A】Misha and Forest
题 题意 有n个点,代号分别为0到n-1,然后这n个点有d个相连点,相连点的XOR sum 为s,求所有的边. 分析 知识:a^b^a=b,a^b^b=a. 把相连点为1的i存进队列,i的唯一相连点就 ...
- Vijos1459 车展 (数学)
描述 遥控车是在是太漂亮了,韵韵的好朋友都想来参观,所以游乐园决定举办m次车展.车库里共有n辆车,从左到右依次编号为1,2,…,n,每辆车都有一个展台.刚开始每个展台都有一个唯一的高度h[i].主管已 ...
- JDBC中prepareStatement 和Statement 的区别
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedSta ...
- POJ2823Sliding Window
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 49919 Accepted: 14391 ...
- goto,void,extern,sizeof实例
1.#include <stdio.h>void func(int n){ int* p = NULL; if( n < 0 ) { goto ST ...
- [转载]AngularJS and scope.$apply
http://jimhoskins.com/2012/12/17/angularjs-and-apply.html http://www.cnblogs.com/zhrj000/p/3383898.h ...
- Windows 下 tail 查看日志命令工具分享
以前在公司时服务器上面可以实现tail 命令查看程序运行日志,感觉相当不错,上网查了下这些命令是linux 下的,还好有好心人开发了一个可以在Windows下的运行的小工具,来给分享一下: 使用方法: ...
- 微信时代,"邮"你选择 腾讯企业邮箱推新玩法
近日,腾讯企业邮箱在广州.北京.南京三地举办<微信时代,“邮”你选择>企业邮箱新方向客户见面会,同时也正式宣布将打通微信.“拥抱”移动办公,领航国内办公工具移动之“变”. 据了解,腾讯企业 ...
- 注意页面上的时间戳可能会成为bd快照的时间_快照不更新的原因
之前在创建内容的时候,为了提高说服力,添加了一个原始文章的地址**.com.cn/2013-08/22/content_**.htm,当时写文章是在12月份,单快照直接变成原始文章的时间戳8.22
- zstu.4189: 逻辑运算(构建 && 前缀表达式入门)
4189: 逻辑运算 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 274 Solved: 42 Description 还记得大学里学过的模电么, ...