LeetCode: Divide Two Integers 解题报告
Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
SOLUTION 1
1. 基本思想是不断地减掉除数,直到为0为止。但是这样会太慢。
2. 我们可以使用2分法来加速这个过程。不断对除数*2,直到它比被除数还大为止。加倍的同时,也记录下cnt,将被除数减掉加倍后的值,并且结果+cnt。
因为是2倍地加大,所以速度会很快,指数级的速度。
3. 另外要注意的是:最小值的越界问题。对最小的正数取abs,得到的还是它。。。 因为最小的正数的绝对值大于最大的正数(INT)
所以,我们使用Long来接住这个集合就可以了。
public class Solution {
public int divide(int dividend, int divisor) {
long a = Math.abs((long)dividend); // ref : http://blog.csdn.net/kenden23/article/details/16986763
// Note: 在这里必须先取long再abs,否则int的最小值abs后也是原值
long b = Math.abs((long)divisor); int ret = 0;
// 这里必须是= 因为相等时也可以减
while (a >= b) {
// 判断条件是 >=
for (long deduce = b, cnt = 1; a >= deduce; deduce <<= 1, cnt <<= 1) {
a -= deduce;
ret += cnt;
}
} // 获取符号位。根据除数跟被除数的关系来定
return (dividend > 0) ^ (divisor > 0) ? -ret: ret;
}
}
注意:
1. C,java中的右移运算,是带符号位的,叫算术右移http://www.cppblog.com/tx7do/archive/2006/10/19/13867.html
2015.1.3 redo:
Leetcode又加强了一大堆边界条件运算,所以我们代码也要更改:
1. 返回值的时候,判断是不是越界,越界返回最大值。
例子:
Input: -2147483648, -1
Expected: 2147483647
public int divide(int dividend, int divisor) {
if (divisor == 0) {
return Integer.MAX_VALUE;
} // Note: 在这里必须先取long再abs,否则int的最小值abs后也是原值
long dividendTmp = Math.abs((long)dividend);
long divisorTmp = Math.abs((long)divisor); // bug 3: ret should use Long to avoid overflow.
long ret = 0;
// bug 2: should use dividentTmp > divisor as the while judge.
while (dividendTmp >= divisorTmp) {
// bug 1: should use Long for tmp.
long tmp = divisorTmp;
int rst = 1;
while(tmp <= dividendTmp) {
// bug 3: the two statement should inside the while LOOP.
ret += rst;
dividendTmp -= tmp; tmp <<= 1;
rst <<= 1;
}
}
// bug 4: out of range:
/*
Input: -2147483648, -1
Output: -2147483648
Expected: 2147483647
*/
//ret = ((dividend > 0) ^ (divisor > 0)) ? -ret: ret;
ret = ((((dividend ^ divisor) >> 31) & 1) == 1) ? -ret: ret; if (ret > Integer.MAX_VALUE || ret < Integer.MIN_VALUE) {
return Integer.MAX_VALUE;
} else {
return (int)ret;
}
}
简化一点:
public int divide(int dividend, int divisor) {
long a = Math.abs((long)dividend);
long b = Math.abs((long)divisor); long ret = 0; while (a >= b) {
for (long tmp = b, cnt = 1; a >= tmp; tmp <<= 1, cnt <<= 1) {
ret += cnt;
a -= tmp;
}
} ret = (((dividend ^ divisor) >> 31) & 1) == 1 ? -ret: ret;
if (ret > Integer.MAX_VALUE || ret < Integer.MIN_VALUE) {
return Integer.MAX_VALUE;
} return (int)ret;
}
GitHub Code:
Ref: http://blog.csdn.net/fightforyourdream/article/details/16899675
LeetCode: Divide Two Integers 解题报告的更多相关文章
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
随机推荐
- BZOJ 1040 ZJOI 2008 骑士 基环树林+树形DP
题目大意:有一些骑士.他们每个人都有一个权值.可是因为一些问题,每个骑士都特别讨厌还有一个骑士.所以不能把他们安排在一起.求这些骑士所组成的编队的最大权值和是多少. 思路:首先貌似是有向图的样子,可是 ...
- 新浪微博 使用OAuth2.0调用API
# -*- coding: cp936 -*- #python 2.7.10 #xiaodeng #新浪微博 使用OAuth2.0调用API #微博开放接口的调用,都需要获取用户的身份认证.目前微博开 ...
- LoadRunner 检查点函数总结
今天我来总结一下Loadrunner中的检查点函数,主要介绍两个函数:web_find()和web_reg_find() 这两个函数均用于内容的查找,但两者也有本质的区别,具体介绍如下: 一.web_ ...
- 转:Windows消息机制要点
Windows消息机制要点 1. 窗口过程 每个窗口会有一个称为窗口过程的回调函数(WndProc),它带有四个参数,分别为:窗口句柄(Window Handle),消息ID(Message ...
- iOS-高仿通讯录之商品索引排序搜索
概述 TableView添加右侧索引, 将数据按照索引分组排序, 并添加搜索功能且在搜索界面复用当前页面. 详细 代码下载:http://www.demodashi.com/demo/10696.ht ...
- js:获取节点相关的 nodeName,nodeType,nodeValue
getElementById() getElementsByName() getElementsByTagName() hasChildNodes() nodeName nodeType=1元素节点/ ...
- eclipse的tasks使用说明
http://blog.csdn.net/limb99/article/details/8881891tasks可以在代码里增加标识,通过tasks view可以快速的找到这些标识的地方,有助于提高开 ...
- C# partial 关键字详解
我们新建一个Windows Form时,后台代码都会自动添加如下代码: public partial class Form1 : Form { public Form1() { InitializeC ...
- oc 代码块的使用
#import <UIKit/UIKit.h> #import "AppDelegate.h" int (^max)(int,int);//定义代码块,类似c的函数指针 ...
- pcurl 学习 http文件下载及写入空设备文件实例
import pycurl import sys import os import random import string import time class Transaction(object) ...