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 ...
随机推荐
- 转载---SQL Server XML基础学习之<5>--XQuery(query)
本章写一些SQL Server XML的一些XQuery基础语法,主要讲的query查询语法 T-SQL 支持用于查询 XML 数据类型的 XQuery 语言的子集. XQuery 基于现有的 XPa ...
- Centos6.5 64linux系统基础优化(二)
1 操作的最小化原则 1)安装系统最小化 2)开启程序服务最小化原则 3)操作最小化原则 4)登陆最小化原则;平时没有需求不用root登陆,要用普通登陆. 2 更改ssh服务默认端口及常规配置 # ...
- iOS 查找字符串 相同 子字符串的位置 range
问题:解决替换同一个字符串的多个相同的字符eg. xxx这个超级大土豪白送xxx一个!赶快来抢把! 将第一个xxx换成名字 将第二个xxx换成物品 两种办法 第二种办法更灵活一点 //第一种办法 ...
- O-C相关-09-id 类型与应用
09-id 类型与应用 1, 使用 NSObject 访问子类对象方法 代码在编辑的时候, Xcode 会实时检查语法情况. 如果调用某个对象的方法, 在声明中没有该方法的声明, 那么就会报错. 但是 ...
- c语言数组不同初始化方式的结果
第一种初始化方式: #include <stdio.h> int main() { int numbers[5]={12,14}; for (int i=0; i<5; i++) { ...
- CCPC网赛,HDU_5832 A water problem
Problem Description Two planets named Haha and Xixi in the universe and they were created ...
- Demo_张仕传_结构体考试-modify
/* 题目: //声明一个结构体类型 struct _AdvTeacher { char *name; char *tile; int age; char *addr; char *p1; //系统预 ...
- 理解Java中的协变返回类型
在面向对象程序设计中,协变返回类型指的是子类中的成员函数的返回值类型不必严格等同于父类中被重写的成员函数的返回值类型,而可以是更 "狭窄" 的类型. Java 5.0添加了对协变返 ...
- softmax
void LogisticRegression_softmax(LogisticRegression *this, double *x) { int i; double max = 0.0; doub ...
- PHP - 传入WebService服务端带中文字符的序列化字串不能反序列化的解决方法
因工作需要,用了web服务,通过远程调用的方式来检索雅虎拍卖数据.前几天遇到一个问题,现在记录一下 客户端: $res = $this->client->call('Get_YahooDa ...