Skip to content

Commit 17c7d2e

Browse files
committed
feat: add solutions
1 parent 7d732f2 commit 17c7d2e

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

clone-graph/shinsj4653.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,39 @@
1212
1313
"""
1414

15+
16+
# Definition for a Node.
17+
class Node:
18+
def __init__(self, val=0, neighbors=None):
19+
self.val = val
20+
self.neighbors = neighbors if neighbors is not None else []
21+
22+
23+
from typing import Optional
24+
from collections import deque
25+
26+
27+
class Solution:
28+
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
29+
30+
if not node:
31+
return
32+
33+
clone = Node(node.val)
34+
clones = {node: clone}
35+
36+
q = deque([node]) # 해당 라인 답지 참고
37+
38+
while q:
39+
node = q.popleft()
40+
41+
for nei in node.neighbors:
42+
if nei not in clones:
43+
clones[nei] = Node(nei.val) # 답지 참고
44+
q.append(nei)
45+
46+
clones[node].neighbors.append(clones[nei]) # 답지 참고
47+
48+
return clone
49+
50+

longest-repeating-character-replacement/shinsj4653.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,17 @@
1111
[회고]
1212
1313
"""
14+
class Solution:
15+
def characterReplacement(self, s: str, k: int) -> int:
16+
max_len = 0
17+
counter = {}
18+
start, end = 0, 0
19+
while end < len(s):
20+
counter[s[end]] = counter.get(s[end], 0) + 1
21+
while end - start + 1 - max(counter.values()) > k:
22+
counter[s[start]] -= 1
23+
start += 1
24+
max_len = max(end - start + 1, max_len)
25+
end += 1
26+
return max_len
1427

reverse-bits/shinsj4653.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,30 @@
1313
"""
1414

1515

16+
class Solution:
17+
def reverseBits(self, n: int) -> int:
18+
st = []
19+
20+
# while n > 0:
21+
# st.append(n % 2)
22+
# n //= 2 => 32 bit 길이 맞춰야함!
23+
24+
while len(st) < 32:
25+
print('st.append: ', n % 2)
26+
st.append(n % 2)
27+
n //= 2
28+
29+
ret, num = 0, 0
30+
print("st: ", st)
31+
32+
# 6 : 110
33+
# [0 1 1]
34+
35+
while st:
36+
print('st.pop(): ', st[-1])
37+
ret += st.pop() * (2 ** num)
38+
num += 1
39+
40+
return ret
41+
42+

0 commit comments

Comments
 (0)