[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 ...
随机推荐
- AOP:spring 的Annotation配置
1.文件目录: 2.实体类 package com.wangcf.po; public class User { private int id; private String name; privat ...
- 关《我是IT小小鸟》有感
我一直认为大学就是一个自由的舒适的学习环境,没有人可以干扰你限制你,以至于我到了大学之后只剩下了颓废的生活.每天上课玩手机,下课玩电脑,吃饭叫外卖,从不去锻炼,周末就熬夜通宵,状态越来越差,导致我逐渐 ...
- c语言学习—图书搜索
请问下:你说的C四大圣经指那几本啊?——<C 陷阱与缺陷> && <C程序设计语言> && <C专家编程> && & ...
- 第五周PSP &进度条
团队项目PSP 一:表格 C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 讨论 讨论用户界面 9:27 10:42 18 57 60 分析与 ...
- Charles安装及使用教程
一. 简介及安装 一.charles的使用 1.1 charles的说明 Charles其实是一款代理服务器,通过过将自己设置成系统(电脑或者浏览器)的网络访问代理服务器,然后截取请求和请求结果达到 ...
- exce族函数详解
exec函数族 函数族说明 fork() 函数用于创建一个新的子进程,该子进程几乎复制了父进程的全部内容,但是,这个新创建的子进程如何执行呢?exec 函数族就提供了一个在进程中启动另一个程序执行的方 ...
- c#对一个类的扩展
首先定义一个静态类,参数使用this约束并选择需要扩展的类,当然也可以 继续添加扩展是需要添加的参数 public static class StringExrprp { /// <summar ...
- JavaScript 保留两位小数
以下我们将为大家介绍 JavaScript 保留两位小数的实现方法: 四舍五入 以下处理结果会四舍五入: var num =2.446242342; num = num.toFixed(2); // ...
- js控制iframe高度自动撑开
<iframe src="index.html" width="100%" name="" id="myiframe&quo ...
- TortoiseSVN的基本使用方法
TotoiseSVN的基本使用方法 来源 https://blog.csdn.net/hecongzhen/article/details/37879801 在 项目管理实践教程一.工欲善其事,必先利 ...