【LeetCode with Python】 Divide Two Integers
来源:互联网 发布:电脑照相机软件下载 编辑:程序博客网 时间:2024/05/01 20:19
博客域名:http://www.xnerv.wang
原题页面:https://oj.leetcode.com/problems/divide-two-integers/
题目类型:
难度评价:★
本文地址:http://blog.csdn.net/nerv3x3/article/details/39453293
原题页面:https://oj.leetcode.com/problems/divide-two-integers/
题目类型:
难度评价:★
本文地址:http://blog.csdn.net/nerv3x3/article/details/39453293
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
class Solution: # @return an integer def divide(self, dividend, divisor): if 0 == divisor or 0 == dividend: return 0 elif 1 == divisor or -1 == divisor: return dividend * divisor return -dividend positive = 1 if (dividend >0 and divisor < 0) or (dividend < 0 and divisor > 0): positive = -1 dividend = dividend if dividend > 0 else -dividend divisor = divisor if divisor > 0 else -divisor result = 0 dividend_now = dividend while dividend_now >= divisor: total = divisor sub_result = 1 while True: total = total << 1 sub_result = sub_result << 1 ## not += 2 if total > dividend_now: total = total >> 1 sub_result = sub_result >> 1 break elif total == dividend_now: break result += sub_result dividend_now -= total return result * positive
0 0
- 【LeetCode with Python】 Divide Two Integers
- [Leetcode]29. Divide Two Integers @python
- [leetcode: Python]29. Divide Two Integers
- LeetCode: Divide Two Integers
- [LeetCode]Divide Two Integers
- LeetCode: Divide Two Integers
- [LeetCode] Divide Two Integers
- [Leetcode] Divide Two Integers
- LeetCode Divide Two Integers
- 【leetcode】Divide Two Integers
- [LeetCode]Divide Two Integers
- LeetCode-Divide Two Integers
- LeetCode: Divide Two Integers
- Divide Two Integers - leetcode
- Leetcode: Divide Two Integers
- LeetCode:Divide Two Integers
- leetcode Divide Two Integers
- LeetCode Divide Two Integers
- 【LeetCode with Python】 Convert Sorted Array to Binary Search Tree
- 【LeetCode with Python】 Copy List with Random Pointer
- 位域之我见
- 【LeetCode with Python】 Count and Say
- 《炉石传说》架构设计赏析(3):Gameplay初探
- 【LeetCode with Python】 Divide Two Integers
- Android SDK目录详解
- 使用Firebug对js进行断点调试的图文方法
- 【LeetCode with Python】 Flatten Binary Tree to Linked List
- postgresql数据导入到mysql
- Android环境搭建之三 Eclipse下搭建Android + C/C++ + OpenCV 开发坏境
- c# 汉字转拼音
- 1~4组成无重复数字的三位数
- 短小精悍的虚拟机:JVM基本结构和功能介绍