[LeetCode] 29. Divide Two Integers ☆☆
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
解法:
这道题让我们求两数相除,而且规定我们不能用乘法,除法和取余操作。
采用位运算中的移位运算,左移一位相当于乘2,右移一位相当于除以2。假设求 a / b,将b左移n位后大于a,则结果 res += 1 << (n - 1),将a更新 (a -= b << (n - 1)) 后进行同样操作,直到 a < b。
public class Solution {
public int divide(int dividend, int divisor) {
if (divisor == 0) {
return Integer.MAX_VALUE;
}
if (dividend == Integer.MIN_VALUE && divisor == -1) {
return Integer.MAX_VALUE;
}
boolean isNeg = (dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0);
long left = Math.abs((long)dividend);
long right = Math.abs((long)divisor);
int result = 0;
while (left >= right) {
int times = 1;
while ((right << times) <= left) {
times++;
}
left -= (right << (times - 1));
result += (1 << (times - 1));
}
return isNeg ? -result : result;
}
}
[LeetCode] 29. Divide Two Integers ☆☆的更多相关文章
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- Java [leetcode 29]Divide Two Integers
题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...
- [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两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)
题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description Problem :不使用乘法,除法,求模计算两个数 ...
- LeetCode: 29. Divide Two Integers (Medium)
1. 原题链接 https://leetcode.com/problems/divide-two-integers/description/ 2. 题目要求 给出被除数dividend和除数divis ...
- [leetcode] 29. divide two integers
这道题目一直不会做,因为要考虑的corner case 太多. 1. divisor equals 0. 2. dividend equals 0. 3. Is the result negative ...
随机推荐
- ElasticSearch之CURL操作(有空再去整理)
https://www.cnblogs.com/jing1617/p/8060421.html ElasticSearch之CURL操作 CURL的操作 curl是利用URL语法在命令行方式下工 ...
- Scrum立会报告+燃尽图(十月二十九日总第二十次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2288 项目地址:https://git.coding.net/zhang ...
- Scrum立会报告+燃尽图(Beta阶段第二周第四次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2412 项目地址:https://coding.net/u/wuyy694 ...
- scrum立会报告+燃尽图(第二周第四次)
此作业要求参考: https://edu.cnblogs.com/campus/nenu/2018fall/homework/2249 一.小组介绍 组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶.公 ...
- 20135208 JAVA第三次实验
课程:Java实验 班级:201352 姓名:贺邦 学号:20135208 成绩: 指导教师:娄佳鹏 实验日期:15.06.03 实验密级: ...
- lintcode-206-区间求和 I
206-区间求和 I 给定一个整数数组(下标由 0 到 n-1,其中 n 表示数组的规模),以及一个查询列表.每一个查询列表有两个整数 [start, end] . 对于每个查询,计算出数组中从下标 ...
- vim 简单用法
vim 是一个纯文本编辑器 模式化的编辑器 1:编辑模式2:输入模式3:末行模式 : 具有命令的接口,在末行模式中可以直接的通过命令修改vim编辑器打开的文本文件 模式转换 1:编辑模式—>输入 ...
- 使用union all 遇到的问题(俩条sql语句行数的和 不等于union all 后的 行数的和 !);遗留问题 怎么找到 相差的呐俩条数据 ?
create table buyer as SELECT b.id AS bankid FROM v_product_deal_main m, base_member b WHERE b.id = m ...
- 【APS.NET Core】- launchSettings.json
launchSettings.json文件为一个ASP.NET Core应用保存特有的配置标准,用于应用的启动准备工作,包括环境变量,开发端口等.在launchSettings.json文件中进行配置 ...
- Linux下修改环境变量PATH
1.什么是环境变量(PATH) 在Linux中,在执行命令时,系统会按照PATH的设置,去每个PATH定义的路径下搜索执行文件,先搜索到的文件先执行. 我们知道查阅文件属性的指令ls 完整文件名为:/ ...