GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/029_Divide_Two_Integers.py at master · terryicoder/leetcode

Expand file tree

/

029_Divide_Two_Integers.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
45 lines (39 loc) · 1.26 KB
/

029_Divide_Two_Integers.py

Copy path

File metadata and controls

45 lines (39 loc) · 1.26 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# class Solution(object):
# def divide(self, dividend, divisor):
# """
# :type dividend: int
# :type divisor: int
# :rtype: int
# """
import math
class Solution(object):
def divide(self, dividend, divisor):
if divisor == 0:
return MAX_INT
if dividend == 0:
return 0
isPositive = (dividend < 0) == (divisor < 0)
m = abs(dividend)
n = abs(divisor)
# ln and exp
res = math.log(m) - math.log(n)
res = int(math.exp(res))
if isPositive:
return min(res, 2147483647)
return max(0 - res, -2147483648)
# def divide(self, dividend, divisor):
# # (dividend >= 0) ^ (divisor < 0)
# isPositive = (dividend < 0) == (divisor < 0)
# dividend, divisor, result = abs(dividend), abs(divisor), 0
# while dividend >= divisor:
# n, nb = 1, divisor
# # fast minus
# while dividend >= nb:
# dividend, result = dividend - nb, result + n
# n, nb = n << 1, nb << 1
# if isPositive:
# return min(result, 2147483647)
# return max(-result, -2147483648)
if __name__ == '__main__':
s = Solution()
print s.divide(1, 1)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.