일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Convert Sorted List to Binary Search Tree
- LeetCode
- Protocol
- data science
- 파이썬
- 715. Range Module
- kaggle
- Class
- Python Implementation
- Python Code
- Decorator
- 315. Count of Smaller Numbers After Self
- Generator
- shiba
- Substring with Concatenation of All Words
- t1
- 밴픽
- 운영체제
- 프로그래머스
- Python
- 시바견
- DWG
- Regular Expression
- 30. Substring with Concatenation of All Words
- 109. Convert Sorted List to Binary Search Tree
- 컴퓨터의 구조
- 43. Multiply Strings
- iterator
- attribute
- concurrency
Archives
- Today
- Total
Scribbling
LeetCode: 29. Divide Two Integers 본문
* Note: shift left is the same as multiplying by two.
class Solution:
def divide(self, dividend: int, divisor: int) -> int:
# Overflow
if dividend == -2**31 and divisor == -1:
return 2**31 - 1
# Positive?
positive = (dividend > 0) is (divisor > 0)
dividend = abs(dividend)
divisor = abs(divisor)
res = 0
subtract = divisor
while divisor <= dividend:
subtract, i = divisor, 1
while subtract <= dividend:
dividend -= subtract
res += i
i <<= 1
subtract <<= 1
if positive:
return res
return -res
'Computer Science > Coding Test' 카테고리의 다른 글
39. Combination Sum (0) | 2021.09.04 |
---|---|
32. Longest Valid Parentheses (0) | 2021.09.04 |
LeetCode: 50. Pow(x, n) (0) | 2021.09.02 |
LeetCode: 30. Substring with Concatenation of All Words (0) | 2021.08.29 |
LeetCode: 26. Remove Duplicates from Sorted Array (0) | 2021.08.27 |