【leetcode】Divide Two Integers (middle)☆
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)☆的更多相关文章
- 【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 ...
- 【leetcode】Combination Sum III(middle)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
- 【leetcode】Repeated DNA Sequences(middle)★
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【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. 思路:不能用 ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 【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 ...
- 【leetcode】Compare Version Numbers(middle)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
随机推荐
- JAVA实现上传下载共享文件
1.上传下载共享文件需要用到jcifs,先下载相关JAR包(开源项目的源码,demo,文挡.API应有尽有) https://jcifs.samba.org/src/
- JavaScript高级程序开发3笔记
Js对象 注意:js基本数据类型不是对象,但是"abc".match()这种,可以调用对象的方法,是因为调用方法是临时产生了一个wrapper的包装对象,this指向它: Js ...
- [Bootstrap]概述
——1.html,css,javascript框架 ——2.一般开发响应式布局或者移动优先的项目可以优先考虑 优点 1.css开发版本(可以直接上手)和源码版本(可根据l ...
- root-systerm-bin是什么program
root-systerm-bin是什么program http://packages.ubuntu.com/lucid/root-system-bin
- union 中可以存储的是不带构造函数的类对象
union 中可以存储的是不带构造函数的类对象 否则不符合逻辑 为什么不符合逻辑?
- Java常见知识问答
1.String.StringBuilder.StringBuffer (1).String是字符串常量,不允许改变 (2).StringBuffer先开辟了一块空间,可以允许改变,即向这个空间添加值 ...
- [CSS]三角形
CSS盒子模型 当我们把padding和width,height全部设置为0,border设为一个较大的像素时 即:我们需要什么方向的三角形,只需把其余的三角形背景色设置为transparent:
- PHP、Java、C#实现URI参数签名算法,确保应用与REST服务器之间的安全通信,防止Secret Key盗用、数据篡改等恶意攻击行为
简介 应用基于HTTP POST或HTTP GET请求发送Open API调用请求时,为了确保应用与REST服务器之间的安全通信,防止Secret Key盗用.数据篡改等恶意攻击行为,REST服务器使 ...
- android 软件开机自启动
安卓的很多功能实现方式都是“Don't call me, I'll call you back!”,开机启动就是其中之一 步骤: 1.首先建立一个BroadcastReceiver, 2.在他的onR ...
- ECSHOP添加购物车加图片飞入效果
为ECSHOP的添加购物车,加入图片飞入效果. 首先: 在goods.dwt中查找添加购物车按钮: 为添加购物车按钮加上id: 例如: <a id="iproduct_{$goods. ...