leetcode 29-> Divide Two Integers without using multiplication, division and mod operator
class Solution(object):
def divide(self, dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
ispositive = True
if dividend > and divisor < :
ispositive = False
if dividend < and divisor > :
ispositive = False
dividend = abs(dividend);divisor = abs(divisor)
if dividend < divisor:
return
num = [,,,,,,,,,]
i =
newdividend = []
while i >= :
tmp =
while dividend >= num[i]:
tmp += ;dividend -= num[i]
newdividend.append(tmp); i -=
tmpm = ; ans = ;i =
while i < :
while tmpm < divisor:
if i > :
break
j = ; t =
while j < and tmpm != :
t += tmpm; j +=
tmpm = t + newdividend[i]; i +=
if tmpm < divisor:
j = ; t =
while j < and ans != :
t += ans; j +=
ans = t
if tmpm >= divisor:
k =
while tmpm >= divisor:
tmpm -= divisor; k +=
j = ; t =
while j < and ans != :
t += ans; j +=
ans = t + k
if ispositive:
if ans > :
return
return ans
if ans >= :
return -
return - ans
leetcode 29-> Divide Two Integers without using multiplication, division and mod operator的更多相关文章
- algorithm@ Divide two integers without using multiplication, division and mod operator. (Bit Operation)
#include<bits/stdc++.h> using namespace std; int divide(int dividend, int divisor) { long long ...
- Divide two integers without using multiplication, division and mod operator.
描述 不能使用乘法.除法和取模(mod)等运算,除开两个数得到结果,如果内存溢出则返回Integer类型的最大值.解释一下就是:输入两个数,第一个数是被除数dividend,第二个是除数divisor ...
- [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 ...
- [LeetCode] 29. Divide Two Integers ☆☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [LeetCode]29. Divide Two Integers两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
随机推荐
- scanf,fscanf,sscanf的区别
scanf是从文件中读 sscanf是从字符串中读 scanf是从键盘输入中读 fread :以字节位计算长度,按照指定的长度和次数读取数据,遇到结尾或完成指定长度读取后停止.fscanf :格式 ...
- 使用 ffmpeg 转换视频格式
ffmpeg 是 *nix 系统下最流行的音视频处理库,功能强大,并且提供了丰富的终端命令,实是日常视频处理的一大利器! 实例 flac 格式转 mp3 音频格式转换非常简单: ffmpeg -i i ...
- linux学习笔记---grep
先来讲讲grep(搜索过滤) 1.命令格式: grep [option] pattern file 2.命令参数: -a 不要忽略二进制的数据 -A<显示行数> 除了显示 ...
- 在centos上搭建Git服务器
第一步:先安装一些相关依赖库和编译工具 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel yum in ...
- bind的封装
1.bind.call.apply三者的区别: 1)bind的返回值是一个函数体,不会被立即调用 2)call.apply会立即调用,第一个参数都是用来改变this的指向,两者的区别是前者传递参数的时 ...
- scrapy instantiation
start from scrapy.cmdline import execute execute(['scrapy', 'crawl', 'jokespider']) items.py import ...
- Windows死机的话,可能的一些猫病
一.由硬件引起的原因 [散热不良] 显示器.电源和CPU在工作中发热量非常大,因此保持良好的通风状况非常重要,如果显示器过热将会导致色彩.图象失真甚至缩短显示器寿命.工作时间太长也会导致电源或显示器散 ...
- php5.3.x连接MS SQL server2008
开篇 因为毕设老师需求的原因,虚拟旅游网站要求的数据库必须使用MS SQL server. 我最擅长的web编程语言是PHP,但是在PHP中链接MS SQL server是一件非常麻烦的事,我个人分析 ...
- 使用注释来解决关于inline-block元素换行问题
昨天群里有人问个问题:为什么button加了文字后,产生了对齐不一致的问题. 原因在于baseline的对齐问题. 然后就有人推荐了一篇文章:关于Vertical-Align你需要知道的事情 其中里面 ...
- Codeforces 765F Souvenirs - 莫队算法 - 链表 - 线段树
题目传送门 神速的列车 光速的列车 声速的列车 题目大意 给定一个长度为$n$的序列,$m$次询问区间$[l, r]$内相差最小的两个数的差的绝对值. Solution 1 Mo's Algorith ...