LeetCode : Divide Two Integers [java]
来源:互联网 发布:java太难了 编辑:程序博客网 时间:2024/06/05 01:14
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
思路:注意使用long进行计算,防止int溢出,然后再判断溢出时重置。
public class Solution {public int divide(int dividend, int divisor) { if(divisor == 0){ return 0; }long a = Math.abs((long)dividend);long b = Math.abs((long)divisor);long res = 0;boolean isPositive = ((dividend ^ divisor) >> 31) == 0;while (a >= b) {long c = b;for (long i = 0; a >= c; i++, c <<= 1) {a -= c;res += 1 << i;if ((isPositive && res > Integer.MAX_VALUE) || (!isPositive && -res < Integer.MIN_VALUE)) {return Integer.MAX_VALUE;}}}return isPositive ? (int) res : (int) -res;}}
1 0
- [Leetcode] Divide Two Integers (Java)
- LeetCode : Divide Two Integers [java]
- Divide Two Integers leetcode java
- LeetCode: Divide Two Integers
- [LeetCode]Divide Two Integers
- LeetCode: Divide Two Integers
- [LeetCode] Divide Two Integers
- [Leetcode] Divide Two Integers
- LeetCode Divide Two Integers
- 【leetcode】Divide Two Integers
- [LeetCode]Divide Two Integers
- LeetCode-Divide Two Integers
- LeetCode: Divide Two Integers
- Divide Two Integers - leetcode
- Leetcode: Divide Two Integers
- LeetCode:Divide Two Integers
- leetcode Divide Two Integers
- LeetCode Divide Two Integers
- HTTP POST请求报文格式分析与Java实现文件上传
- 使用友盟集成微信登录时的一些注意点
- storm文档(7)----基本概念
- 有趣的 Scala 语言: 函数成了一等公民
- FFTW编译对应Windows lib32或lib64库文件
- LeetCode : Divide Two Integers [java]
- maven 项目(一) 多项目多层级搭建配置及其优点
- 阿里云的OSS的Java_SDK使用时遇到的问题
- vim 常用命令
- PHP GIF 动画缩略图
- HashMap和Hashtable的区别
- 优秀博客一览
- hibernate标准查询
- 各主板U盘启动热键