`
testforvln
  • 浏览: 18938 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • superich2008: 1、去重可以用Set集合2、在排序后,相邻2个元素如果相同可以 ...
    4Sum

Divide Two Integers

 
阅读更多
自己实现除法 太太太恶心了。。。。
就是用位移代替了乘法,然后不断减去位移得到的乘数*除数
主要是测试数据还有一个Java.MinValue的问题 = = 取了abs之后它居然还是负数。。。我日
public class Solution {
    public int divide(int dividend, int divisor) {
        // Start typing your Java solution below
        // DO NOT write main() function
        int positive = 1;
        if (dividend >0 && divisor < 0 || dividend <0 && divisor > 0 )
            positive = -1;
        if (divisor == Integer.MIN_VALUE) {
            if (dividend == Integer.MIN_VALUE) {
                return 1;
            }
            else {
                return 0;
            }
        }
        if (dividend == Integer.MIN_VALUE) {
            if (positive == -1) {
                return -1 + divide(dividend + divisor, divisor);
            }
            else {
                return 1 - divide(dividend + divisor, divisor);
            }
        }
        dividend = Math.abs(dividend);
        divisor = Math.abs(divisor);
        int result = 0;
        
        while (dividend >= divisor){
            int product = 1;
            int temp = divisor;
            while (dividend - temp >= temp){
                product = product << 1;
                temp = temp << 1;
            } 
            result += product;
            dividend -= temp;
        }
        
        return positive * result;
    }
}
分享到:
评论

相关推荐

    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