[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 ...
随机推荐
- Android_神奇的android:clipChildren属性
正文 一.效果图 看到这个图时你可以先想想如果是你,你怎么实现这个效果.马上想到用RelativeLayout?NO,NO,NO,,, 二.实现代码 <?xml version="1. ...
- hey
<div style="height:547px;"> <!--用来保留原来的位置,如果不加的话会脱离流媒体--> <div class=" ...
- jquerymobile listview 局部刷新
function onSuccess(data, status) { data = $.trim(data); // alert(data); // return; if (data) { $('#l ...
- modbus rtu 协议转DLT645-2007和DLT645-1997电表协议转换器定制,
现场会碰到现场数据为Modbus协议,但是后台系统为DLT645协议系统,本模块支持将工业ModbusRtu协议转换为电表国标协议DLT645协议,支持1997和2007俩种标准,只需要进行简单的配置 ...
- json、map互转
首先,json转map 方法一: Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); 或 Gs ...
- HCE:Host-based Card Emulation基于Android设备的卡片模拟器
HCE技术支持提供了一个软实现SE的通路,Service实现的方式很多,可以使用文件,使用网络,甚至连接真正的SE.支持HCE的测试手机:目前可以确定使用了NXP PN547作为CLF的NFC手机已经 ...
- 论山寨手机与Android 【12】3G时代SmartPhone BP部分
最成熟的3G网络系统,是3GPP项目组制订的WCDMA.WCDMA的网络结构,可参考Figure 12.1,其中有几个特点. 1. 反向兼容GSM/GRPS网络. 原有GSM网络的基站子系统(BSS) ...
- react-native学习笔记——ViewStack组件
今天来写一个组件,相信很多人都会用到的——ViewStack. ViewStack组件无疑是UI中很重要的一个组件,可惜react-native并没有内嵌进去,需要开发者自己去实现. 实现原理很简单, ...
- 在一个frame设置四个组件
import javax.swing.*; import java.awt.event.*; import java.awt.*; class TouChaCol{ JFrame frame; JLa ...
- C/C++各种系统开发环境搭建
http://pan.baidu.com/s/1qWJKF4g