Skip to content

Commit 881c58d

Browse files
author
wuduhren
committed
updates
1 parent ab00993 commit 881c58d

5 files changed

+181
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Codec:
2+
def encode(self, strs: List[str]) -> str:
3+
output = ''
4+
for string in strs:
5+
output += str(len(string))+'#'+string
6+
return output
7+
8+
9+
def decode(self, s: str) -> List[str]:
10+
output = []
11+
i = 0
12+
13+
while i<len(s):
14+
j = i+1
15+
16+
while s[j]!='#': j += 1
17+
18+
l = int(s[i:j])
19+
string = s[j+1:j+1+l]
20+
output.append(string)
21+
i = j+1+l
22+
23+
return output

problems/python3/group-anagrams.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3+
def normalize(string: str) -> str:
4+
counter = collections.Counter()
5+
ans = ''
6+
7+
for c in string:
8+
counter[c] += 1
9+
10+
for c in 'abcdefghijklmnopqrstuvwxyz':
11+
if counter[c]>0:
12+
ans += c+str(counter[c])
13+
return ans
14+
15+
group = collections.defaultdict(list)
16+
ans = []
17+
18+
for string in strs:
19+
group[normalize(string)].append(string)
20+
21+
for normalizedString in group:
22+
ans.append(group[normalizedString])
23+
return ans
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
#left[i] := product of all nums left of nums[i] (not include nums[i])
4+
left = [1]
5+
temp = 1
6+
for num in nums:
7+
temp *= num
8+
left.append(temp)
9+
10+
#right[i] := product of all nums right of nums[i] (not include nums[i])
11+
right = []
12+
temp = 1
13+
for num in reversed(nums):
14+
temp *= num
15+
right.append(temp)
16+
right.reverse()
17+
right.append(1)
18+
right = right[1:]
19+
20+
ans = []
21+
for i in range(len(nums)):
22+
ans.append(left[i]*right[i])
23+
return ans
24+
25+
#In Place
26+
class Solution:
27+
def productExceptSelf(self, nums: List[int]) -> List[int]:
28+
N = len(nums)
29+
ans = [0]*N
30+
31+
#generate "left"
32+
ans[0] = 1
33+
for i in range(1, N):
34+
ans[i] = ans[i-1]*nums[i-1]
35+
36+
#generate "right"
37+
temp = 1
38+
for i in range(N-2, -1, -1):
39+
temp *= nums[i+1]
40+
ans[i] *= temp
41+
42+
return ans
43+
44+
45+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#Bucket Sort
2+
class Solution:
3+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
4+
bucket = collections.defaultdict(list)
5+
counter = collections.Counter(nums)
6+
ans = []
7+
8+
for num in counter:
9+
bucket[counter[num]].append(num)
10+
11+
tempCount = len(nums)
12+
while len(ans)<k:
13+
ans.extend(bucket[tempCount])
14+
tempCount -= 1
15+
16+
return ans
17+
18+
#QuickSelect
19+
class Solution:
20+
def topKFrequent(self, nums: List[int], K: int) -> List[int]:
21+
def quickSelect(freqs, s, e, K):
22+
i = s
23+
t = s
24+
j = e
25+
pivot = freqs[(s+e)//2][1]
26+
27+
while t<=j:
28+
if freqs[t][1]<pivot:
29+
freqs[i], freqs[t] = freqs[t], freqs[i]
30+
i += 1
31+
t +=1
32+
elif freqs[t][1]>pivot:
33+
freqs[j], freqs[t] = freqs[t], freqs[j]
34+
j -= 1
35+
else:
36+
t += 1
37+
if e-j>=K:
38+
return quickSelect(freqs, j+1, e, K)
39+
elif e-(i-1)>=K:
40+
return pivot
41+
else:
42+
return quickSelect(freqs, s, i-1, K-(e-i+1))
43+
44+
counts = collections.Counter(nums)
45+
freqs = [(num, counts[num]) for num in counts]
46+
ans = []
47+
48+
KthLargestFreq = quickSelect(freqs, 0, len(freqs)-1, K)
49+
50+
for num, freq in freqs:
51+
if freq>=KthLargestFreq:
52+
ans.append(num)
53+
return ans

problems/python3/valid-sudoku.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution:
2+
def isValidSudoku(self, board: List[List[str]]) -> bool:
3+
def rowIsValid(i):
4+
used = set()
5+
for j in range(N):
6+
if board[i][j]=='.': continue
7+
if board[i][j] in used: return False
8+
used.add(board[i][j])
9+
return True
10+
11+
def columnIsValid(i):
12+
used = set()
13+
for j in range(N):
14+
if board[j][i]=='.': continue
15+
if board[j][i] in used: return False
16+
used.add(board[j][i])
17+
return True
18+
19+
def isBoxValid(i1, i2, j1, j2):
20+
used = set()
21+
for i in range(i1, i2+1):
22+
for j in range(j1, j2+1):
23+
if board[i][j]=='.': continue
24+
if board[i][j] in used: return False
25+
used.add(board[i][j])
26+
return True
27+
28+
N = 9
29+
for i in range(N):
30+
if not rowIsValid(i): return False
31+
if not columnIsValid(i): return False
32+
33+
for i in range(0, N, 3):
34+
for j in range(0, N, 3):
35+
if not isBoxValid(i, i+2, j, j+2): return False
36+
37+
return True

0 commit comments

Comments
 (0)