题目来源:

  https://leetcode.com/problems/divide-two-integers/


题意分析:

  不用乘法,除法和mod运算来实现一个除法。如果数值超过了int类型那么返回int的最大值。


题目思路:

  初步来说,有两个做法。

  ①模拟除法的过程,从高位开始除,不够先右挪一位。这种方法首先要将每一位的数字都先拿出来,由于最大int类型,所以输入的长度不超过12位。接下来就是模拟除法的过程。

  ②利用左移操作来实现出发过程。将一个数左移等于将一个数×2,取一个tmp = divisor,所以将除数tmp不断左移,直到其大于被除数dividend,然后得到dividend - tmp,重复这个过程。


代码(python):

 class Solution(object):
def divide(self, dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
ispositive = True
if dividend > 0 and divisor < 0:
ispositive = False
if dividend < 0 and divisor > 0:
ispositive = False
dividend = abs(dividend);divisor = abs(divisor)
if dividend < divisor:
return 0
num = [1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000]
i = 9
newdividend = []
while i >= 0:
tmp = 0
while dividend >= num[i]:
tmp += 1;dividend -= num[i]
newdividend.append(tmp); i -= 1
tmpm = 0; ans = 0 ;i = 0
while i < 10:
while tmpm < divisor:
if i > 9:
break
j = 0; t = 0
while j < 10 and tmpm != 0:
t += tmpm; j += 1
tmpm = t + newdividend[i]; i += 1
if tmpm < divisor:
j = 0; t = 0
while j < 10 and ans != 0:
t += ans; j += 1
ans = t
if tmpm >= divisor:
k = 0
while tmpm >= divisor:
tmpm -= divisor; k += 1
j = 0; t = 0
while j < 10 and ans != 0:
t += ans; j += 1
ans = t + k
if ispositive:
if ans > 2147483647:
return 2147483647
return ans
if ans >= 2147483648:
return -2147483648
return 0 - ans

模拟过程

 class Solution(object):
def divide(self, dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
ispositive = True
if dividend > 0 and divisor < 0:
ispositive = False
if dividend < 0 and divisor > 0:
ispositive = False
dividend = abs(dividend);divisor = abs(divisor)
if dividend < divisor:
return 0
tmp = divisor
ans = 1
while dividend >= tmp:
tmp <<= 1
if tmp > dividend:
break
ans <<= 1
tmp >>= 1
nans = ans + self.divide(dividend - tmp,divisor)
if ispositive:
if ans > 2147483647:
return 2147483647
return nans
if ans >= 2147483648:
return -2147483648
return 0 - nans

左移


转载请注明出处:http://www.cnblogs.com/chruny/p/4893254.html

[LeetCode]题解(python):029-Divide Two Integers的更多相关文章

  1. [Leetcode][Python]29: Divide Two Integers

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...

  2. 【LeetCode】029. Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  3. Java for LeetCode 029 Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  4. LeetCode 029 Divide Two Integers

    题目要求:Divide Two Integers Divide two integers without using multiplication, division and mod operator ...

  5. leetcode第28题--Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 分析:题目意思很容易理解,就是不用乘除法和模运 ...

  6. LeetCode(29)Divide Two Integers

    题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...

  7. 029 Divide Two Integers 两数相除

    不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...

  8. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  9. leetcode面试准备:Divide Two Integers

    leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...

  10. 【一天一道LeetCode】#29. Divide Two Integers

    一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...

随机推荐

  1. php执行shell更新svn文件的方法

    vim /etc/sudoers 修改内容如下: #Defaults !visiblepw Defaults visiblepw #Defaults requiretty <?php set_t ...

  2. 张王李相亲应用if else

    package hello; public class to { public static void main(String[]args){ int a =1,b=0; int c =1,d=0; ...

  3. 第七届河南省赛10403: D.山区修路(dp)

    10403: D.山区修路 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 69  Solved: 23 [Submit][Status][Web Bo ...

  4. java中File类详解

    构造函数 代码如下: public class FileDemo {     public static void main(String[] args){         //构造函数File(St ...

  5. 深入理解 IE haslayout

    转载自Bubblings Blog 原文地址:http://riny.net/2013/haslayout/ 1.什么是haslayout layout是windows IE的一个私有概念,它决定了元 ...

  6. 如何分割(split)string字符串

    使用String#split()方法 如下所示: String string = "004-034556"; String[] parts = string.split(" ...

  7. 一个简单顺序表的C++实现

    /* SList.cpp Author: Qiang Xiao Time: 2015-07-11 */ #include<iostream> using namespace std; ; ...

  8. MyEclipse 在loading workbench 启动卡死

    解决方法: 找到Workspace目录,在.metadata(Mac 下是在 .metadata/.plugins)中删掉以下两个文件 org.eclipse.ui.workbench org.ecl ...

  9. codeforces 374D. Inna and Sequence 线段树

    题目链接 给m个数, n个操作, 一个数列, 初始为空.一共有3种操作, 在数列末尾加0, 加1, 或删除位置为a[i]的数, a[i]为初始给的m个数, 如果a[i]大于数列长度, 那么什么也不发生 ...

  10. 神奇的魔法数字0x61c88647

    来源JDK源码,产生的数字分布很均匀 用法代码如下. # -*- coding: utf-8 -*- HASH_INCREMENT = 0x61c88647 def magic_hash(n): fo ...