Skip to content

Commit de6765a

Browse files
authored
Add files via upload
1 parent b175cd0 commit de6765a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2209
-0
lines changed

more/math/addOperators.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
"""
3+
class Solution:
4+
def addOperators(self, num: str, target: int) -> List[str]:
5+
if len(num) == 0:
6+
return []
7+
8+
self.ans = []
9+
10+
def dfs(idx, path):
11+
if idx == len(num) - 1:
12+
path += num[idx]
13+
if eval(path) == target:
14+
self.ans.append(path)
15+
return
16+
17+
dfs(idx + 1, path + num[idx] + "+")
18+
dfs(idx + 1, path + num[idx] + "-")
19+
dfs(idx + 1, path + num[idx] + "*")
20+
if (num[idx] != '0' or (path and path[-1] not in ['-','+','*'] and num[idx]=='0')):
21+
dfs(idx + 1, path + num[idx])
22+
23+
24+
dfs(0, "")
25+
26+
return self.ans
27+
28+
class Solution:
29+
def addOperators(self, num: 'str', target: 'int') -> 'List[str]':
30+
31+
ans = []
32+
33+
def dfs(i, prev, cur, val, eq):
34+
if i == len(num):
35+
if val == target and cur == 0:
36+
ans.append("".join(eq[1:]))
37+
return
38+
39+
cur = cur *10 + int(num[i])
40+
str_op = str(cur)
41+
42+
if cur > 0:
43+
dfs(i+1, prev, cur, val, eq)
44+
45+
dfs(i+1, cur, 0, val + cur, eq + "+" + str_op)
46+
47+
if eq:
48+
dfs(i+1, -cur, 0, val - cur, eq + '-' + str_op)
49+
dfs(i+1, cur * prev, 0, val - prev + (cur*prev), eq + '*' + str_op)
50+
51+
52+
dfs(0,0,0,0,"")
53+
54+
return ans
55+

more/math/basicCalculator.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def calculate(self, s: str) -> int:
3+
s += '$'
4+
def helper(stk, i):
5+
num = 0
6+
sign = '+'
7+
while i < len(s):
8+
c = s[i]
9+
if c == " ":
10+
i += 1
11+
continue
12+
elif c.isdigit():
13+
num = num * 10 + int(c)
14+
i += 1
15+
elif c == '(':
16+
num, i = helper([], i+1)
17+
else:
18+
if sign == '+':
19+
stk.append(num)
20+
if sign == '-':
21+
stk.append(-num)
22+
sign = c
23+
num = 0
24+
i += 1
25+
if c == ')':
26+
return sum(stk), i
27+
28+
return sum(stk)
29+
30+
return helper([],0)

more/math/basicCalculator2.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Basic Calculator II
3+
"""
4+
class Solution:
5+
def calculate(self, s: str) -> int:
6+
s += '$'
7+
sign = '+'
8+
i = num = 0
9+
stk = []
10+
11+
while i < len(s):
12+
c = s[i]
13+
if c == " ":
14+
i += 1
15+
continue
16+
elif c.isdigit():
17+
num = num * 10 + int(c)
18+
i += 1
19+
else:
20+
if sign == '+':
21+
stk.append(num)
22+
if sign == '-':
23+
stk.append(-num)
24+
if sign == '*':
25+
stk.append(stk.pop() * num)
26+
if sign == '/':
27+
stk.append(int(stk.pop() / num))
28+
29+
sign = c
30+
num = 0
31+
i += 1
32+
33+
return sum(stk)
34+

more/math/basicCalculator3.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
def calculate(self, s: str) -> int:
3+
s += '$'
4+
def helper(stk, i):
5+
sign = '+'
6+
num = 0
7+
while i < len(s):
8+
c = s[i]
9+
if c == " ":
10+
i += 1
11+
continue
12+
elif c.isdigit():
13+
num = num * 10 + int(c)
14+
i += 1
15+
elif c == '(':
16+
num, i = helper([], i+1)
17+
else:
18+
if sign == '+':
19+
stk.append(num)
20+
if sign == '-':
21+
stk.append(-num)
22+
if sign == '*':
23+
stk.append(stk.pop() * num)
24+
if sign == '/':
25+
stk.append(int(stk.pop() / num))
26+
i += 1
27+
num = 0
28+
if c == ')':
29+
return sum(stk), i
30+
sign = c
31+
return sum(stk)
32+
return helper([],0)

more/math/daysBetweenDates.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[date1,date2] = sorted([date1,date2])
2+
y1,m1,d1 = list(map(int,date1.split('-')))
3+
y2,m2,d2 = list(map(int,date2.split('-')))
4+
self.day31 = {1,3,5,7,8,10,12}
5+
if y1==y2:
6+
return self.get_day(y2,m2,d2) - self.get_day(y1,m1,d1)
7+
else:
8+
sm = self.day_of_year(y1) - self.get_day(y1,m1,d1)
9+
sm += self.get_day(y2,m2,d2)
10+
for y in range(y1+1,y2):
11+
sm += self.day_of_year(y)
12+
return sm
13+
14+
def get_day(self, year, month, day):
15+
sm = 0
16+
for m in range(1,month):
17+
sm += self.day_of_month(year,m)
18+
sm += day
19+
return sm
20+
21+
def day_of_month(self,year,month):
22+
if month in self.day31:
23+
return 31
24+
elif month == 2:
25+
if self.day_of_year(year)==366:
26+
return 29
27+
else:
28+
return 28
29+
else:
30+
return 30
31+
32+
def day_of_year(self,year):
33+
if year%100==0:
34+
if year%400==0:
35+
return 366
36+
else:
37+
return 365
38+
else:
39+
if year%4==0:
40+
return 366
41+
else:
42+
return 365

more/math/evalRPN.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def evalRPN(self, tokens: List[str]) -> int:
3+
stk = []
4+
5+
while tokens:
6+
c = tokens.pop(0)
7+
if c not in '+-/*':
8+
stk.append(int(c))
9+
else:
10+
a = stk.pop()
11+
b = stk.pop()
12+
13+
if c == '+':
14+
stk.append(a + b)
15+
if c == '-':
16+
stk.append(b-a)
17+
if c == '*':
18+
stk.append(a * b)
19+
if c == '/':
20+
stk.append(int(b / a))
21+
22+
return stk[0]

more/math/fractionToDecimal.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def fractionToDecimal(self, numerator: int, denominator: int) -> str:
3+
4+
n,r = divmod(abs(numerator), abs(denominator))
5+
res = []
6+
remainders = {}
7+
8+
if numerator * denominator < 0:
9+
res.append('-')
10+
11+
res.append(str(n))
12+
13+
if r > 0:
14+
res.append(".")
15+
16+
while r > 0 and r not in remainders:
17+
remainders[r] = len(res)
18+
r *= 10
19+
n, r = divmod(r, abs(denominator))
20+
res.append(str(n))
21+
22+
if r in remainders:
23+
idx = remainders[r]
24+
res.insert(idx,'(')
25+
res.append(')')
26+
27+
return "".join(res)

more/math/isNumber.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def isNumber(self, s: str) -> bool:
3+
met_e = met_p = met_d = False
4+
5+
s = s.strip()
6+
7+
for i, c in enumerate(s):
8+
if c == 'e':
9+
if met_e or met_d == False:
10+
return False
11+
met_e = True
12+
met_d = False
13+
elif c == '-' or c == '+':
14+
if i > 0 and s[i-1] != 'e':
15+
return False
16+
elif c == '.':
17+
if met_p or met_e:
18+
return False
19+
met_p = True
20+
elif c.isdigit():
21+
met_d = True
22+
else:
23+
return False
24+
25+
return met_d
26+
27+

more/math/isRobotBounded.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def isRobotBounded(self, instructions: str) -> bool:
3+
x, y, dx, dy = 0, 0, 0, 1
4+
5+
for i in instructions:
6+
if i == 'G':
7+
x += dx
8+
y += dy
9+
elif i == 'L':
10+
dx, dy = dy, -dx
11+
elif i == 'R':
12+
dx, dy = -dy, dx
13+
14+
return (x,y)==(0,0) or (dx, dy) != (0, 1)

more/math/leastInterval.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
time: n
3+
space: (1)n
4+
"""
5+
# CPU Task Scheduler
6+
class Solution:
7+
def leastInterval(self, tasks: List[str], n: int) -> int:
8+
task_counts = list(collections.Counter(tasks).values())
9+
mxCnt = max(task_counts)
10+
numTsks = task_counts.count(mxCnt)
11+
return max(len(tasks), (mxCnt-1)*(n+1) + numTsks)

0 commit comments

Comments
 (0)