[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的更多相关文章
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- 【LeetCode】29. Divide Two Integers
题意:不用乘除求余运算,计算除法,溢出返回INT_MAX. 首先考虑边界条件,什么条件下会产生溢出?只有一种情况,即返回值为INT_MAX+1的时候. 不用乘除求余怎么做? 一.利用减法. 耗时太长, ...
- 29. Divide Two Integers - LeetCode
Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...
- leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- Java [leetcode 29]Divide Two Integers
题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
随机推荐
- 解决Firefox下input button内文字垂直居中
众所周知,在Firefox下input type=”button”的文字是不好居中的,原因在于Firefox自己比较二,弄了个私有属性,导致以下问题的出现: 按钮左右本身有2px的间距(FF私有属性写 ...
- python-凯撒密码
凯撒密码 简介:凯撒密码(Caesar)是最早的代换密码,对称密码的一种 算法:将每个字母用字母表中它之后的第k(称作位移值)个字母替代 代码: #-*-coding:utf-8-*- __autho ...
- eclipse IDE 扩展pydev
1. 安装PyDev. 运行Eclipse,打开菜单Help->Install New Software.在work with里输入网址:http://pydev.org/updates ,然后 ...
- leetcode算法刷题(四)——动态规划(二)
又到了晚上,动态规划,开刷! 第121题 Best Time to Buy and Sell Stock 题目的意思:给予一个数组price,表示特定股票在某天的股价,里面第i个数表示第i天的价格.只 ...
- Three.js基础
Three.js基础探寻一 Three.js基础探寻一 1.webGL 一种网络标准,定义了一些较底层的图形接口. 2.Three.js 一个3Djs库,webGL开源框架中比较优秀的一个.除了w ...
- 在Linux下sqlplus 登录时显示SID 和用户名
一般显示为: SQL> show user USER 为 "SYS" SQL> 在 $ORACLE_HOME/sqlplus/admin目录下 编辑glogin.sql ...
- hdu 1078(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 //dp[i][j]表示从点i,j处开始能获得的最多cheese #include <io ...
- Xshell中文乱码
终端”编码设置,默认是 默认语言,选择UTF8设置即可
- xshell + xmanger连接centos gnome+ kde桌面 for需要X window的App
- 开源DirectShow分析器和解码器: LAV Filter
LAV Filter 是一款开源的DirectShow视频分离和解码软件,他的分离器LAVSplitter封装了FFMPEG中的libavformat,解码器LAVAudio和LAVVideo则封装了 ...