29. Divide Two Integers
用加减法模拟除法。
除法本质就是 被除数 - 商个除数相加 = 0
如果你电脑足够好,可以无限减。。但是这个题肯定不是这么简单。
最快的方法还是 减去 商乘以除数。
但是这里不能使用乘法,那只好用BIT的运算来实现了。
自己没做出来,但是发现一刷做出来了,怎么看都不像是我这个智商能写出来的,所以不知道当时看的哪的答案,贴出的答案如有冒犯,请告之。
比如19/3,只要分子比分母大,就可以除。
19 比 3大, 比2个还3大,牛逼牛逼,比4个3大,你敢信?比8个3小。。
那么19先减去4个3肯定没错,此时分子是19-4*3=7;结果是4,已经4个3了。
然后再看7/3,7比1个3大,比2个3大,那么分子就是7-2*3 = 1,结果是2+刚才的4=6。
然后1/3,比3小了,让你装逼。此时停止,结果是第一次的2+刚才的4=6.
所以就是通过BIT位操作来看剩下的分子比几个分母大,正常乘法是1234567个这么测试,测1次就行了,但是用BIT是测很多次,每次1248个,最后相加。
然后各种edge case好贱。
public class Solution {
public int divide(int dividend, int divisor)
{
if(dividend == 0) return 0;
long up = (long)dividend;
long down = (long)divisor;
boolean pos = (up*down) >= 0;
up = Math.abs(up);
down = Math.abs(down);
long res = 0;
while(up >= down)
{
int numOfDowns = 1;
while(up > (down<<1))
{
down <<= 1;
numOfDowns <<= 1;
}
up -= down;
res += (long)numOfDowns;
down = Math.abs((long)divisor);
}
if(res == (long)Integer.MIN_VALUE*(-1) && pos) return Integer.MAX_VALUE;
if(pos) return (int)res;
else return (int)res*-1;
}
}
29. Divide Two Integers的更多相关文章
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- 29. Divide Two Integers - LeetCode
Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...
- [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
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- 29. Divide Two Integers (JAVA)
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, 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 ...
- 29. Divide Two Integers (INT; Overflow, Bit)
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- javascript 不间断向左滚动图片
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- shell 1变量注意点
定义变量时,变量名不加美元符号($),如: variableName="value" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样. 删除变量 使用 un ...
- str、__str__ 、repr、 __repr__、``
内建函数str()和repr() 或反引号操作符(``)可以方便地以字符串的方式获取对象的内容.类型.数值属性等信息. str()函数得到的字符串可读性好(故被print调用),而结果通常无法用eva ...
- General Purpose Hash Function Algorithms
General Purpose Hash Function Algorithms post@: http://www.partow.net/programming/hashfunctions/inde ...
- adb的logcat使用
预备:安装刷机精灵,实用工具->adb命令行 1. 对于多机设备,首先使用adb devices来获知设备名称: 2. 将log输出到电脑:adb –s [设备名称] shell logcat ...
- info.plist 属性讲解
1 常用项: Application requires iPhone environment:如果应用程序不能在ipodtouch上运行,设置此项为true; Application usesWi-F ...
- base64的一个应用情景
AddActivity.xml rushrank.xml 不过AddActivity.xml不也是通过二进制流就传过去了吗? 事实上是可以的,只要不将这些二进制的数据写下来,传播是可以的,只 ...
- nutch-1.7 编译
转载自:http://peigang.iteye.com/blog/1563288 从nutch-.3开始 本地抓取(单击) 和 分布式抓取(集群)所使用的配置文件和命令单独分开. 资源:下载地址:h ...
- spm使用之七不用seajs改用headjs起步
这几天在看phpwind官方网站, 他们的前端用了一个叫做head.js的js加载器, 官方网站在 http://headjs.com/ 号称是你只需要在你的html文件中的<head>& ...
- Git如何Check Out出指定文件或者文件夹
原文 http://www.handaoliang.com/a/20140506/195406.html 在进行项目开发的时候,有时候会有这样的需求那就是:我们只希望从Git仓库里取指定的文件或者文 ...