Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

思路:

尼玛,各种通不过,开始用纯减法,超时了。

然后用递归,溢出了。

再然后终于开窍了,用循环,把被除数每次加倍去找答案,结果一遇到 -2147483648 就各种不行, 主要是这个数一求绝对值就溢出了。

再然后,受不了了,看答案。 发现,大家都用long long来解决溢出。看得我欲哭无泪啊。

综合后AC的代码:

int divide(int dividend, int divisor) {
if(divisor == )
return dividend;
if(dividend == - && abs(divisor) == )
return ; int sign = (dividend > ^ divisor > ) ? - : ;
long long ans = ;
long long absdivisor = abs((long long)divisor);
long long absdividend = abs((long long)dividend);
long long t = absdivisor;
long long n = ;
while(absdividend >= t + t) //被除数每次加倍,找到可以加到的最大值
{
t = t << ;
n = n << ;
}
while(absdividend >= absdivisor) //从可以减的最大值开始,每次减,并把除数还原一部分
{
if(absdividend >= t)
{
absdividend -= t;
ans += n;
}
n = n >> ;
t = t >> ;
} return sign * ans;
}

【leetcode】Divide Two Integers (middle)☆的更多相关文章

  1. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  2. 【leetcode】Combination Sum III(middle)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

  4. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  5. 【leetcode】Balanced Binary Tree(middle)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  6. 【leetcode】Set Matrix Zeroes(middle)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...

  7. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  8. 【leetcode】 search Insert Position(middle)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. 【leetcode】Compare Version Numbers(middle)

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

随机推荐

  1. Debian8 加载NTFS 磁盘出错 解决方法

    执行 ntfsfix  /dev/sdb2 // sd*  a代表第一块硬盘 b代表第2块硬盘 数字是分区号 执行后 就可以正常使用了.

  2. selenium Grid(一)

    selenium grid Quick Start selenium-grid是用于设计帮助我们进行分布式测试的工具,其整个结构是由一个hub节点和若干个代理节点组成.hub用来管理各个代理节点的注册 ...

  3. iOS - 视图与手势(UIview & UIGestureRecognizer)

    01 UIView视图的基本使用 --- 在根视图中添加2个UIView视图 //视图确实加载时调用 - (void)viewDidLoad { [super viewDidLoad]; // Do ...

  4. Cocos2d-x使用UserDefault数据持久化实例:保存背景音乐和音效设置

    UserDefault可以实现数据的存储,但是它的使用不能泛滥,具体讲一般情况下不会使用它保存大量的数据,它没有SQL语句那样的灵活.UserDefault除了保存游戏设置外,还有可以长期保持游戏精灵 ...

  5. ios 多线程-GCD-NSOperation

    一.线程间的通讯 1.使用NSObject类的方法performSelectorInBackground:withObject:来创建一个线程. 具体的代码:隐式创建,自动启动 [Object per ...

  6. 很牛逼的android真机调试,手机、平板、电视都可

    1.首先通过路由器,搭建局域网 2.手机开wifi,记住ip地址,平板开wifi,记住ip地址,电视开wifi,记住ip 3.然后再eclipse里面“窗口-首选项-android-ddms里面设置使 ...

  7. Poj OpenJudge 百练 2632 Crashing Robots

    1.Link: http://poj.org/problem?id=2632 http://bailian.openjudge.cn/practice/2632/ 2.Content: Crashin ...

  8. input onfocus onblur

    <input type="text" style="color:#999" value="账户" onfocus='if(this.v ...

  9. python中文件的复制

    python中文件的复制 python的os模块有很多文件目录相关的函数,但没有提供直接复制文件的函数,当然可以通过边都边写的方式复制文件.想要直接复制文件可以通过shutil模块 shutil模块是 ...

  10. IOS 透​视​投​影​矩​阵​推​导(转)

    http://wenku.baidu.com/link?url=wDkyQR9fDI_tZas1BlMRUnNNoKwiQDygltm2wWxRr_sDwcDB51_QCDfR4Gb5wYrIUZ_t ...