首页 > 代码库 > [Leetcode][Python]29: Divide Two Integers
[Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-
‘‘‘
__author__ = ‘dabay.wang@gmail.com‘
29: Divide Two Integers
https://oj.leetcode.com/problems/divide-two-integers/
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
===Comments by Dabay===
先确定符号。
做除法式子:
一个字符串保存商,一个字符串保存余数。
计算每一位商的时候,用余数来减除数,如果剩下的数大于0,商就加1。
最后返回的时候,考虑题目中提到的overflow。(个人感觉这个要求没什么意义)
‘‘‘
class Solution:
# @return an integer
def divide(self, dividend, divisor):
if dividend == 0:
return 0
if (dividend > 0 and divisor < 0) or (dividend < 0 and divisor > 0):
quotient = "-"
else:
quotient = ""
dividend = abs(dividend)
divisor = abs(divisor)
dividend = str(dividend)
i = 0
remainder = ""
while i < len(dividend):
remainder = int(str(remainder) + dividend[i])
quot = 0
while remainder >= divisor:
remainder -= divisor
quot += 1
quotient += str(quot)
i += 1
if int(quotient) > 2147483647:
return 2147483647
elif int(quotient) < -2147483648:
return 2147483648
else:
return int(quotient)
def main():
sol = Solution()
print sol.divide(1, -1)
if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[Leetcode][Python]29: Divide Two Integers
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。