Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b9ee027

Browse files
committed
[20220826] leetcode study-plan 문제 풀이
1 parent 5fffdb0 commit b9ee027

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# https://leetcode.com/problems/is-subsequence/
2+
# 392. Is Subsequence
3+
4+
class Solution:
5+
def isSubsequence(self, s: str, t: str) -> bool:
6+
i, j = 0, 0
7+
while i < len(s) and j < len(t):
8+
if s[i] == t[j]:
9+
i += 1
10+
j += 1
11+
else:
12+
j += 1
13+
14+
return i == len(s)
15+
16+
17+
if __name__ == '__main__':
18+
sol = Solution()
19+
print(sol.isSubsequence("abc", "ahbgdc"))
20+
print(sol.isSubsequence("axc", "ahbgdc"))
21+
print(sol.isSubsequence("b", "abc"))
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# https://leetcode.com/problems/isomorphic-strings/
2+
# 205. Isomorphic Strings
3+
4+
from collections import defaultdict
5+
6+
7+
class Solution:
8+
def isIsomorphic(self, s: str, t: str) -> bool:
9+
sdic = defaultdict()
10+
tdic = defaultdict()
11+
if len(s) != len(t):
12+
return False
13+
14+
for a, b in zip(s, t):
15+
sdic[a] = b
16+
tdic[b] = a
17+
18+
for a, b in zip(s, t):
19+
if sdic[a] != b or tdic[b] != a:
20+
return False
21+
22+
return True
23+
24+
25+
if __name__ == '__main__':
26+
sol = Solution()
27+
print(sol.isIsomorphic("egg", "add"))
28+
print(sol.isIsomorphic("foo", "bar"))
29+
print(sol.isIsomorphic("paper", "title"))
30+
print(sol.isIsomorphic("badc", "baba"))
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# https://leetcode.com/problems/merge-two-sorted-lists/
2+
# 21. Merge Two Sorted Lists
3+
4+
from typing import Optional
5+
6+
7+
# Definition for singly-linked list.
8+
class ListNode:
9+
def __init__(self, val=0, next=None):
10+
self.val = val
11+
self.next = next
12+
13+
14+
class Solution:
15+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
16+
# ListNode 생성 하여 풀이 (AC)
17+
18+
if list1 is not None and list2 is None:
19+
return list1
20+
21+
if list1 is None and list2 is not None:
22+
return list2
23+
24+
if list1 is None and list2 is None:
25+
return None
26+
27+
start = dummy = ListNode()
28+
29+
while list1 is not None and list2 is not None:
30+
if list1.val < list2.val:
31+
start.next = list1
32+
list1 = list1.next
33+
elif list1.val == list2.val:
34+
start.next = list1
35+
list1 = list1.next
36+
else:
37+
start.next = list2
38+
list2 = list2.next
39+
start = start.next
40+
41+
if list1 is not None and list2 is None:
42+
start.next = list1
43+
44+
if list2 is not None and list1 is None:
45+
start.next = list2
46+
47+
return dummy.next
48+
49+
def mergeTwoLists_2(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
50+
# ListNode 생성 없이 풀이 (Discuss 참고)
51+
# 작은 값을 가진 리스트 노드를 이동 시켜서 연결 시키면 됨
52+
53+
# list1이 None 이면 list2 가 list1의 가장 큰 값 보다 크다는 의미이므로
54+
# list2 를 리턴시킨다.
55+
if list1 is None:
56+
return list2
57+
58+
if list2 is None:
59+
return list1
60+
61+
if list1.val < list2.val:
62+
# list1을 이동시킴
63+
# 그리고 결과로 나온 값 (최소 l1 보다 큰 값) 을 l1 에 이어 붙인다.
64+
list1.next = self.mergeTwoLists_2(list1.next, list2)
65+
return list1
66+
else:
67+
# list2 이동
68+
# 같으면 사실 list1 이든 list2 든 누가 이동해도 상관 없음 (어느쪽에 붙여든 상관 없음)
69+
list2.next = self.mergeTwoLists_2(list1, list2.next)
70+
return list2
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# https://leetcode.com/problems/reverse-linked-list/
2+
# 206. Reverse Linked List
3+
4+
from typing import Optional
5+
6+
7+
# Definition for singly-linked list.
8+
class ListNode:
9+
def __init__(self, val=0, next=None):
10+
self.val = val
11+
self.next = next
12+
13+
def __str__(self) -> str:
14+
return str(self.val)
15+
16+
17+
class Solution:
18+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
19+
20+
if head is None:
21+
return None
22+
23+
start = dummy = ListNode()
24+
25+
stack = []
26+
while head is not None:
27+
stack.append(head.val)
28+
head = head.next
29+
30+
while stack:
31+
start.next = ListNode(stack.pop())
32+
start = start.next
33+
34+
return dummy.next
35+
36+
def reverseList_2(self, head: Optional[ListNode]) -> Optional[ListNode]:
37+
# TODO stack 사용 없이 풀어보기
38+
pass
39+
40+
41+
if __name__ == '__main__':
42+
sol = Solution()
43+
cur = node = ListNode()
44+
for i in range(1, 6):
45+
node.next = ListNode(i)
46+
node = node.next
47+
48+
print(sol.reverseList_2(cur.next))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /