【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 ...
随机推荐
- C# 线程--第三线程池
概述 线程池有那些优点: 1.在多线程中线程池可以减少我们创建线程,并合理的复用线程池中的线程.因为在线程池中有线程的线程处于等待分配任务状态. 2.不必管理和维护生存周期短暂的线程,不用在创建时为其 ...
- Java WebService简单实例
一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 1.创建[Web Service Project],命名为[TheService ...
- <%@include和<jsp:include
博客地址:http://www.cnblogs.com/shizhongtao/p/3506742.html欢迎交流 <%@ include %>是编译时包含,<jsp:includ ...
- 上下问语句句柄Release地方
OCI--在QUERY中 CLI--在FETCH中 在父类中定义了public—Release和protected—Release,protected—Release在public—Release中被 ...
- struts2 的action 向页面传值
写一个Action类: public class LoginAction{ public String execute(){ return SUCCESS; } public void setValu ...
- [DevExpress]RepositoryItemComboBox 数据绑定
关键代码: public static void Bind<T>(this RepositoryItemComboBox combox, ICollection source) { /*说 ...
- Sublime Text 3 使用备注
去年开始为了正规化自己的日常编辑工作,在dw,editplus,notap++,st里做了个选择,最终决定改曾经的dw为st. 毕竟dw是上个世纪的东西了,体积比较臃肿了.所以,在这里记录关于st的使 ...
- SQL Server 2000 “用户XX已经存在” 处理方法
-- 目前遇到这个问题都是在切换服务器时发生的. 旧服务器备份的数据库还原到新服务器,都会遇到这种问题 --切决方案如下: -- 查找孤立用户列表 EXECUTE sp_change_users_lo ...
- 《C和指针》 读书笔记 -- 第7章 函数
1.当程序调用一个无法见到原型的函数时,编译器便认为该函数返回一个整型值.如果这个值实际上是非整型值时,还得执行类型转换,所以函数原型声明有时很重要. 2.值的类型并不是值的内在本质,而是取决于它被使 ...
- what is the virtual machine, when and why we need use it ?
虚拟机(Virtual Machine)指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算机系统. 通过虚拟机软件,你可以在一台物理计算机上模拟出二台或多台虚拟的计算机,这些虚 ...