Maximum Common Divisor (最大公约数)
1 | // Function to return gcd of a and b |
149. Max Points on a Line
Calculate GCD:
1 | def gcd(a, b): |
Calculation Problems
43. Multiply Strings
- Use an
length = m + n
array to record all the sums - The product of
num1[i] * num2[j]
will locate at[i + j, i + j + 1]
- char to int:
str1.charAt(2) - '0'
1 | class Solution { |
Palindrome Problems
9.Palindrome Number
[Solution] Compare half of the number with another half
1 | class Solution { |
829. Consecutive Numbers Sum
Leetcode: https://leetcode.com/problems/consecutive-numbers-sum/description/
Solution: From length = 1, find all the combinations that satisfies the condition.
(Time Limit Exceeds)1
2
3
4
5
6
7
8
9
10
11
12
13
14def consecutiveNumbersSum(self, N: int) -> int:
l = 1
cnt = 0
while True:
sum = (1 + l) * l // 2
if sum > N:
break
while sum <= N:
if sum == N:
cnt += 1
break
sum += l
l += 1
return cnt
(Optimized but also TLE)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16def consecutiveNumbersSum(self, N: int) -> int:
if N == 1:
return 1
l = 1
cnt = 0
while l <= N / 2 + 1:
cur = N // l
if cur < l / 2:
l += 1
continue
if l % 2 == 0 and (cur * 2 + 1) / 2 * l == N:
cnt += 1
elif l % 2 == 1 and cur * l == N:
cnt += 1
l += 1
return cnt
https://zhanghuimeng.github.io/post/leetcode-295-find-median-from-data-stream/1
2
3
4
5
6
7
8
9
10
11def consecutiveNumbersSum(self, N: int) -> int:
cnt = 0
l = 1
while True:
cur = N - l * (l - 1) / 2
if cur <= 0:
break
if cur % l == 0:
cnt += 1
l += 1
return cnt
273. Integer to English Words
Leetcode: https://leetcode.com/problems/integer-to-english-words/
[Solution]
- Use 1000 as a base
- Use the helper function to calculate results within [1, 999]
1 | class Solution(object): |
Probablity
528. Random Pick with Weight
Leetcode: https://leetcode.com/problems/random-pick-with-weight/
Pick random numbers from array with probability.
等比数列
1 | SUM = (a1 * q^n - a1) / (q - 1) |
Calculator problems
224. Basic Calculator
Leetcode: https://leetcode.com/problems/basic-calculator/
[Solution]
- The character could only be
(
,)
,0-9
,+
,-
num
represents current number- Use
1
and-1
to represent+-
operations
Note: The stack is used to store number, and operators before (. We start traversing the input string, we use num
to store the current number, if we meet with +
or -
, we add the current number to the previous result in the stack, and update the oper
for the next calculation. When we meet (
, we want to remember what operator is before the (
, thus, we push current operator into stack, at the same time, in order to avoid directly add operator numbers, we push another 0
into the stack, as the initial value of the current calculation. In the end, when we meet )
, we want to pop
the previous result from the stack, add num
to it, and update num
. Also, we pop
again from the stack, this time, the popped value would be the operator before the (
.
1 | def calculate(self, s: str) -> int: |
227. Basic Calculator II
Leetcode: https://leetcode.com/problems/basic-calculator-ii/
Note:
- Do the calculation before inserting into the stack
- Use stack[-1] as previous number
- Only store numbers in stack
- Note that when operator is
/
, we need to deal with different situations (when result < 0) since the “integer division should truncate toward zero” oper
means how we need to deal with the current number
1 | def calculate(self, s: str) -> int: |
394. Decode String
Leetcode: https://leetcode.com/problems/decode-string/
Note: to check if a character is character:
1 | if ch.isalpha(): |
1 | def decodeString(self, s: str) -> str: |
772. Basic Calculator III
Leetcode: https://leetcode.com/problems/basic-calculator-iii/
679. 24 Game(hard)
Leetcode: https://leetcode.com/problems/24-game/
Note:
- For each two combinations in
nums
, calculate their ALL possible results (includinga - b
andb - a
), append their result to the next round, and keep iterating
1 | def judgePoint24(self, nums: List[int]) -> bool: |