题目来源:

  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. cloneNode小结

    js原生API中有个cloneNode,还有一个可选的参数, true代表复制子节点,包括任何包裹在标签之间的东西,当然包括文本节点,也就是标签之间有什么,它就会不假思索的全部都克隆一份. false ...

  2. Java中synchronized注意点

    之前一直以为 synchronized 加在方法前面就只有一个线程能访问了,项目中碰到一个问题,一个类的不同对象,同时访问加了 synchronized的方法 同样是可以访问的,那是因为 synchr ...

  3. Wide character in print at hcp.pl line 21.

    jrhmpt01:/root# cat -n hcp.pl 1 use LWP::UserAgent; 2 use Encode; 3 $ua = LWP::UserAgent->new; 4 ...

  4. Uber 司机有话说:你以为当个 Uber 司机很轻松?大错特错!

    Uber 最近的负面新闻越来越多.各方成员都在抨击.斥责.揭露 Uber 公司的各种黑幕.今天,来自 Uber 公司的司机为您讲述咱「拼车老司机」自己的故事.你以为开着自己的私家车出去满城市的晃悠接客 ...

  5. jquery.cookie用法详细解析,封装的操作cookie的库有jquery.cookie.js

    jquery.cookie用法详细解析 需要注意存入cookie前,对数据进行序列化, 得到后在反序列化: 熟练运用:JSON.stringify();和JSON.parse(): 通常分为如下几个步 ...

  6. c 跟字符串有关的函数

    1.字符串查找 strstr char * strstr(const char *s1, const char *s2); 在s1中查找s2,如果找到返回首个s2的首地址 char * strcase ...

  7. 关于javascript面向对象之闭包

    要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量,而在函数外部无法 ...

  8. linux下core文件调试方法(转载)

    转自于:http://blog.csdn.net/fcryuuhou/article/details/8507775 在程序遇到段错误不寻常退出时,一般是访问内存出错.但是不会给出程序哪里出现的问题, ...

  9. 【转】使用Boost Graph library(二)

    原文转自:http://shanzhizi.blog.51cto.com/5066308/942972 让我们从一个新的图的开始,定义一些属性,然后加入一些带属性的顶点和边.我们将给出所有的代码,这样 ...

  10. perl5 第一章 概述

    第一章 概述 by flamephoenix 一.Perl是什么?二.Perl在哪里?三.运行四.注释 一.Perl是什么?      Perl是Practical Extraction and Re ...