`

Divide Two Integers

阅读更多
Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

题目要求我们不用乘法,除法还有模运算来完成两个整数的除运算。如果溢出时返回最大值。

题目要求返回一个整型,当除数为Integer.MIN_VALUE,被除数为-1时就会越界,这个情况我们要单独处理。因为不能用乘法,除法和模运算,因此我们想到的就是位运算来处理,左移一位相当于乘2,右移一位相当于除2。但是运算过程中,如果左移可能会溢出,因此我们要对这种情况考虑,可以把变量声明为long。具体代码如下:
public class Solution {
    public int divide(int dividend, int divisor) {
        if(divisor == -1 && dividend == Integer.MIN_VALUE) 
            return Integer.MAX_VALUE;
        long dsor = Math.abs((long) divisor);
        long dend = Math.abs((long) dividend);
        int result = 0;
        while(dend >= dsor) {
            int counter = 0;
            while(dend >= (dsor << counter)) {
                counter ++;
            }
            counter --;
            result += 1 << counter;
            dend -= dsor << counter;
        }
        if(dividend < 0 && divisor > 0 || dividend > 0 && divisor < 0) 
            return -result;
        return result;
    }
}
0
0
分享到:
评论

相关推荐

    leetcode叫数-python01:Python01

    先来看LeetCode-29上的Divide Two Integers题目要求: Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 1 2 3 就是说不用乘法,除法,求模运算...

    cpp-算法精粹

    Divide Two Integers Text Justification Max Points on a Line Community QQ 群: 237669375 Github: https://www.github.com/soulmachine/algorithm-essentials 微博: @灵魂机器 License Book License: CC ...

    lovely-nuts:这是一个可爱的坚果

    Practice-Leetcode 这是一个Chinese School Girl:China:用来练习leetcode的文档....29.Divide Two Integers 二进制累加代替除法,防止溢出 36.Valid Sudoku set去重复 2018/04/19: 038.Count and Say 递归 040.C

    LeetCode最全代码

    371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](./C++/sum-of-two-integers.cpp) [Python](./Python/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Easy | LintCode | ...

    丢失的最小正整数leetcode-interview-algorithm:面试时的JS算法

    Divide Two Integers 两个整数相除,要求不能使用 *,/,以及mod操作符,返回除数,若除数有小数点,只返回整数部分,如2.789,只应返回2,此题为leetcode上的题目 Random Numbers 用计算机生成了N个1到1000之间的...

    Dir-For-LeetCode

    问题 完全的 017_Letter_Combinations_of_a_Phone_Number 018_4总和 019_Remove_Nth_Node_From_End_of_List 020_Valid_Parentheses ... 029_Divide_Two_Integers 030_Substring_with_Concatenation_of

    HugeInteger大整数计数器作业

    HugeInteger Class) Create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each. Provide member functions input, output, add and subtract. For ...

    LeetCode 29. 两数相除

    题目来源:https://leetcode-cn.com/problems/divide-two-integers 题目 给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。 返回被除数 dividend 除以除数 divisor ...

    leetcode切割分组-leetcode:leetcode

    029_divide_two_integers*.py # 实现整除 050_pow.py # 实现乘幂 066_plus_one.py # 数列末尾值+1 069_sqrt.py # 实现开根号 136_single_number.py # 位操作:异或(xor)操作 x ^ 0 = x; x ^ x = 0 sum 001_two_sum....

    复杂度测试 Performance Measurement

    The recursive method is to equally divide the integer set into two parts by the integer in the middle position. Then recursively print the first part, followed by printing the integer in the middle, ...

    Microsoft Library MSDN4DOS.zip

    Chapter 4 Defining and Using Integers Chapter 5 Defining and Using Complex Data Types Chapter 6 Using Floating-Point and Binary Coded Decimal Numbers Chapter 7 Controlling Program Flow Chapter 8 ...

    语言程序设计课后习题答案

    第 一 章 概述 1-1 简述计算机程序设计语言的发展历程。 解: 迄今为止计算机程序设计语言的发展经历了机器语言、汇编语言、高级语言等阶段,C++语言是一种面向对象的编程语言,也属于高级语言。...

    The Art of Assembly Language Programming

    The ASCII Character Set &lt;br&gt;1.12 Summary 1.13 Laboratory Exercises 1.13.1 Installing the Software 1.13.2 Data Conversion ... 1.14 Questions 1.15 Programming Projects &lt;br&gt;Chapter Two ...

Global site tag (gtag.js) - Google Analytics