[LeetCode]题解(python):029-Divide Two Integers
题目来源:
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的更多相关文章
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- 【LeetCode】029. Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- Java for LeetCode 029 Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- LeetCode 029 Divide Two Integers
题目要求:Divide Two Integers Divide two integers without using multiplication, division and mod operator ...
- leetcode第28题--Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 分析:题目意思很容易理解,就是不用乘除法和模运 ...
- LeetCode(29)Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
- 029 Divide Two Integers 两数相除
不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
随机推荐
- ubuntu安装XHProf
1. 安装XHProf wget http://pecl.php.net/get/xhprof-0.9.2.tgz tar zxf xhprof-0.9.2.tgz cd xhprof-0.9.2 s ...
- 一个简单的文本编辑器。(是在DEV C++下写的)
//头文件// main.h #define CM_FILE_SAVEAS 9072 #define CM_FILE_EXIT 9071 #define CM_FILE_OPEN 9070 #defi ...
- iOS设计模式——单例模式
单例模式用于当一个类只能有一个实例的时候, 通常情况下这个“单例”代表的是某一个物理设备比如打印机,或是某种不可以有多个实例同时存在的虚拟资源或是系统属性比如一个程序的某个引擎或是数据.用单例模式加以 ...
- golang printf
1: 打印包括字段在内的实例的完整信息 同 %+V fmt.Printf("Hello world! %v","hufeng") 输出:Hello world ...
- 关于给javascript对象添加、删除、修改对象的属性
以下是自己总结的几种方法 利用动态特性 function Person(){}; var person = new Person(); person.name = 'yy'; person.gende ...
- PHPExcel 导出
<?php include '../init.inc.php'; include "../db.inc.php"; /* @func 引入类 */ include ROOT. ...
- BZOJ 1012 最大数maxnumber
Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. ...
- [C#绘图]在半透明矩形上绘制字符串
首先要绘制一个半透明的矩形,用到的方法当然是FillRectangle().这个函数在调用的时候除了要指明要绘制的矩形外,还要指明填充矩形的背景色.具体的方法就是在绘制矩形的时候传给它一个画刷Brus ...
- cocos2d-x教程1 hello world
HelloworldScene.h #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos ...
- Latex调整行距
修改行间距的方法: \usepackage{setspace}%使用间距宏包 \begin{document} \begin{spacing}{2.0}%%行间距变为double-space 双倍行距 ...