【LeetCode】29. Divide Two Integers

题意:不用乘除求余运算,计算除法,溢出返回INT_MAX。

  首先考虑边界条件,什么条件下会产生溢出?只有一种情况,即返回值为INT_MAX+1的时候。

  不用乘除求余怎么做?

  一.利用减法。

    耗时太长,如果被除数是INT_MIN,除数是1的时候,要循环-INT_MIN次

  二.利用位运算

  思路来自:http://blog.csdn.net/whuwangyi/article/details/40995863

  代码:

  

 1 int divide(int dividend, int divisor) {
 2     int i=0;
 3     long ret=0;
 4     int flag=1;
 5     long dividend_copy=dividend;
 6     long divisor_copy=divisor;
 7     if((dividend<0&&divisor>0)||(dividend>0&&divisor<0))
 8         flag=-1;
 9     dividend_copy=dividend<0?((-1)*dividend_copy):dividend_copy;
10     divisor_copy=divisor<0?((-1)*divisor_copy):divisor_copy;
11     while(dividend_copy>=divisor_copy<<1){
12         divisor_copy<<=1;
13         i++;
14     }
15     while(i>=0){
16         if(dividend_copy>=divisor_copy){
17             dividend_copy-=divisor_copy;
18             ret+=1<<i;
19         }
20         i--;
21         divisor_copy>>=1;
22     }
23     ret = (flag<0)?((-1)*ret):ret;
24     if(ret==INT_MAX+1)
25         return INT_MAX;
26     else
27         return (int)ret;
28 }
时间: 2024-12-02 16:25:43

【LeetCode】29. Divide Two Integers的相关文章

[Leetcode][Python]29: Divide Two Integers

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 29: Divide Two Integershttps://oj.leetcode.com/problems/divide-two-integers/ Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT. ===C

【Leetcode】Sum of Two Integers

题目链接:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3. 思路: 唉,这题虽然是easy,但是真好烦的一题,之前在hihocoder(还是其他oj)上好像也做过这

LeetCode --- 29. Divide Two Integers

题目链接:Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 这道题的要求是在不使用乘法.除法.取模运算的前提下实现两个整数相除.如果溢出,返回MAX_INT. 这道题的直接思路是用被除数不断减去除数,直到为0.这种方法的迭代次数是结果的大小,即比如结果为n,算法复杂度是O(n). 可以

LeetCode开心刷题十六天——29. Divide Two Integers*

From now on,I grade the questions I've done,* less means more difficult *** done by myself **need see answer,but I can reappear it *need see answer&hard to reappear 29. Divide Two Integers Medium 7003349FavoriteShare Given two integers dividend and d

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

【LeetCode】Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example:A = 

【LeetCode】Sort Colors

LeetCode OJ Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, w

【LeetCode】【Python题解】Pascal&#39;s Triangle

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] 要求输入一个整数,返回一个表示杨辉三角的数组.我的方法是计算通项公式,首先是编写阶乘函数,然后计算C00,C10,C11即可 利用Python 的map嵌套可以很简洁地实现,核心代码只有一行! class So

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"