leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers
1 题目
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
接口: public int divide(int dividend, int divisor)
2 思路
题意
不用乘、除、mod
做一个除法运算。
解
直接用除数去一个一个加,直到被除数被超过的话,会超时。
解决办法:每次将被除数增加1倍,同时将count也增加一倍,如果超过了被除数,那么用被除数减去当前和再继续本操作。
复杂度: O(n)
3 代码
public int divide(int dividend, int divisor) {
// 处理负数
int xor = dividend ^ divisor;
int signal = xor >= 0 ? 1 : -1;
// 正常逻辑
long count = 0;
long num = Math.abs((long) dividend), div = Math.abs((long) divisor);
long tmp = div;
while (num >= tmp) {
long countTmp = 1;
while (num >= tmp) {
tmp = tmp << 1;
countTmp = countTmp << 1;
}
count += countTmp >> 1;
tmp = tmp >> 1;
num = num - tmp;
tmp = div;
}
// 处理溢出的特殊用例{-2147483648, -1}
long res = count * signal;
return res > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)res;
}
4 总结
数学思维,考智商。
leetcode面试准备:Divide Two Integers的更多相关文章
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- LeetCode OJ:Divide Two Integers(两数相除)
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【LeetCode】029. Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【LeetCode】29. Divide Two Integers
题意:不用乘除求余运算,计算除法,溢出返回INT_MAX. 首先考虑边界条件,什么条件下会产生溢出?只有一种情况,即返回值为INT_MAX+1的时候. 不用乘除求余怎么做? 一.利用减法. 耗时太长, ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- LeetCode: Divide Two Integers 解题报告
Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...
- 【Leetcode】【Medium】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
随机推荐
- 获取通讯录的信息(关于iOS9.0之后新的框架-ContactFramework)
转载自:http://my.oschina.net/u/2340880/blog/511995?p={{totalPage}} 一.引言 在以前iOS开发中,涉及联系人相关的编程,代码都非常繁琐,并且 ...
- 一些简单的帮助类(1)-- String的类型验证
在工作中经常会遇到 验证String 中的值是否属于Int型或者是Bool又或是Date 一般的做法是用方法 类型.TryParse(string,类型) 来做验证. "; int intV ...
- HOW TO: Creating your MSI installer using Microsoft Visual Studio* 2008
Quote from: http://software.intel.com/en-us/articles/how-to-creating-your-msi-installer-using-visual ...
- HTTP中Get与Post、ViewState 原理
Http是请求,响应的模型,服务器不会来读取浏览器的网页,只能够得到客户端提交过来的数据当用户点击提交,服务器就知道"提交回来了"(PostBack) Get与Post 设置for ...
- Linux 内核学习的经典书籍及途径
from:http://www.zhihu.com/question/19606660 知乎 Linux 内核学习的经典书籍及途径?修改 修改 写补充说明 举报 添加评论 分享 • 邀请回答 ...
- important的妙用
!important: 为某些样式设置具有最高权值,高于id选择器 用法: !important要写在分号的前面 例如: <p class="first">!impor ...
- javascript下动态this与动态绑定实例代码
this 的值取决于 function 被调用的方式,一共有四种, 如果一个 function 是一个对象的属性,该 funtion 被调用的时候,this 的值是这个对象. 如果 function ...
- js data日期初始化的5种方法new Date()
var objDate=new Date([arguments list]); 参数形式有以下5种: 1)new Date("month dd,yyyy hh:mm:ss"); 2 ...
- mysql扩展库操作mysql数据库
环境搭建 启用mysql扩展库,在php.ini文件中去配置mysql扩展库 extension=php_mysql.dll 查询数据库 1.建库建表 //建库testcreate database ...
- 【javascript 变量和作用域】
今天学习了javascript 的变量和作用域的基本知识,对于以前在开发中遇到的一些不懂的小问题也有了系统的认识,收获还是比较多的. [基本类型和引用类型] ECMAScript 变量可能包含两种不同 ...