【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 ...
随机推荐
- 值类型与引用类型(特殊的string) Typeof和GetType() 静态和非静态使用 参数传递 相关知识
学习大神博客链接: http://www.cnblogs.com/zhili/category/421637.html 一 值类型与引用类型 需要注意的string 是特殊类型的引用类型. 使用方法: ...
- String.IsNullOrWhiteSpace和String.IsNullOrEmpty的区别
以前刚入行的时候判断字符串的时候用 string a="a"; a==""; a==null; 后来发现了String.IsNullOrEmpty感觉方便了好多 ...
- CSS的浮动和清除
CSS浮动和清除 什么是浮动? 在实现效果上,让元素浮起来(飘起来),动起来(向左或向右移动) 浮动本质:就是将两个块元素同存一行. float 取值:主要是对浮动的方向进行控制 left:让元素向左 ...
- <%@include和<jsp:include
博客地址:http://www.cnblogs.com/shizhongtao/p/3506742.html欢迎交流 <%@ include %>是编译时包含,<jsp:includ ...
- 写在十年 2007-09-15 (写给L之三)
你知道吗? 那种时间很远,但心很近的感觉. 时间已经远去了十年, 但亲切的感觉依然清晰可见, 无论时光远去了十年,二十年,三十年, 永远…… 它已经植根,在心间……
- 韩顺平细说Servlet视频系列之tom相关内容
韩顺平细说Servlet视频系列之tom相关内容 tomcat部署项目操作(注意:6.0版本以后的支持该操作,5.x版本需要另外配置?待验证!) 项目发布到tomcat的webapps文件下,然后启动 ...
- SQL SERVER 遇到Unable to find the requested .Net Framework Data Provider. It may not be installed. (System.Data)
今天新装的SQLSERVER 2012 EXPRESS 用于客户端程序 安装完成后打开登陆SQLSERVER 一切正常 当查看表定义.视图结构时,弹出一下内容 Unable to find the r ...
- 2014-10 u-boot make过程分析
/** ****************************************************************************** * @author Maox ...
- windows github 搭建与使用
git/github使用以下是全部在命令行使用(windows/github) 注册账户以及创建仓库先在github建立账号和创建仓库,此时为空的仓库 配置git下载并安装 git windows版本 ...
- 【winform】如何在DateTimePicker中显示时分秒
1. 首先属性里面的Format属性value设置为Custom(默认为Long) 2. 对应的Custom属性value设置为yyyy-MM-dd HH:mm:ss