29. Divide Two Integers - LeetCode
Question
Solution
题目大意:给定两个数字,求出它们的商,要求不能使用乘法、除法以及求余操作。
思路:说下用移位实现的方法
7/3=2,7是被除数,3是除数
除数左移,假设移动了n次后,移到最接近被除数,这时被除数=被除数-除数,商的一部分为2^n
如果被除数>除数,则继续循环
除数左移,又移动了m次后,移到最接近被除数,这时被除数=被除数-除数,商的一部分为2^m
最后商为2^n+2^m+...
Java实现:
法1:如果可以用除法,一步就可以了
public int divide2(int dividend, int divisor) {
// overflows
if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE;
// 给定两个数字,求出它们的商,要求不能使用乘法、除法以及求余操作。
return dividend / divisor;
}
法2:下面是用减法实现的,执行超时
public int divide2(int dividend, int divisor) {
// overflows
if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE;
int ans = 0;
boolean negative = !((dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0));
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
dividend -= divisor;
while (dividend >= 0) {
ans++;
dividend -= divisor;
}
return negative ? -ans : ans;
}
法3:用移位实现
public int divide(int dividend, int divisor) {
// 防止溢出
if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE;
// 获取最终结果的符号
int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;
long dvd = Math.abs((long) dividend);
long dvs = Math.abs((long) divisor);
int ans = 0;
while (dvd >= dvs) {
long tmp = dvs, multiple = 1;
while (dvd >= (tmp << 1)) {
tmp <<= 1;
multiple <<= 1;
}
dvd -= tmp;
ans += multiple;
}
return sign == 1 ? ans : -ans;
}
29. Divide Two Integers - LeetCode的更多相关文章
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- 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 ☆☆
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 ...
随机推荐
- 前馈控制+PID
参考来源: 北京交通大学 硕士学位论文 基于脉冲串控制的含位置反馈和前馈补偿的位置控制算法的研究 赵旺升
- 《基于.NET Core构建微服务》系列文章(更新至第6篇,最新第7篇,已发布主页候选区)
原文:Building Microservices On .NET Core – Part 1 The Plan 时间:2019年1月14日 作者:Wojciech Suwała, Head Arch ...
- 记离线缓存(manifest)一大坑,断定其只适用于静态网站或离线应用
今天看了离线缓存(manifest)方面的资料,兴冲冲地就想给自己的网站用上.待我把代码都写好部署上服务器,并测试过OK的时候,在SegmentFault刷了一把manifest方面的问答,才发现这个 ...
- 用jq实现移动端滑动轮播以及定时轮播效果
Html的代码: <div class="carousel_img"> <div class="car_img" style="ba ...
- 利用Charles做代理测试电脑上写的H5页面
做H5页面的同学可能经常会遇到一个场景,就是电脑上调试好的页面怎么在手机上访问测试呢? 下面就介绍一种自己经常使用的方式,利用Charles代理软件来实现! 安装Charles 直接去官网下载对应的系 ...
- Unknown host mirrors.opencas.cn You may need to adjust the proxy settings in Gradle 报错及解决办法
亲测Unknown host mirrors.opencas.cn You may need to adjust the proxy settings in Gradle 解决办法 - 程序员大本营 ...
- mysql find_in_set在oracle下的解决方案
比如一张表: artile (id,type,content); type:1表示文艺类,2表示小说类,3表示传记,4表示传说,等等5,6,7,8 表数据: id type content 1 3,1 ...
- re模块补充与其他模块介绍
注:昨日写了re单个模块几个重要的点需要补充 一.re模块补充 1.findall独有的优先级别展示 res = re.findall('abc', 'abcabcabcabc') print(res ...
- (更新中)"华为杯" 武汉大学21级新生程序设计竞赛 非官方题解
"华为杯" 武汉大学21级新生程序设计竞赛 https://ac.nowcoder.com/acm/contest/31620#question D.和谐之树 思路:披着线段树外皮 ...
- 玩转LiteOS组件:玩转Librws
摘要:Librws是一个跨平台的websocket客户端,使用C语言编写. 本文分享自华为云社区<LiteOS组件尝鲜-玩转Librws>,作者: W922 . 本期小编为大家带来Lite ...