-
-
Notifications
You must be signed in to change notification settings - Fork 195
[suwi] Week 04 #836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[suwi] Week 04 #836
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
666ad79
merge-two-sorted-lists solution
sungjinwi 726459c
missing-number solution
sungjinwi 32e1e4c
created files
sungjinwi 333c656
word-search solution
sungjinwi 7f4e8ad
word-search solution
sungjinwi 5c4c12a
Merge branch 'DaleStudy:main' into main
sungjinwi 06701b1
delete empty palindrome-substrings
sungjinwi 929ae16
Merge branch 'main' of https://github.com/sungjinwi/leetcode-study
sungjinwi c2689a9
Refactor : use list1&& list2 instead make new instance
sungjinwi 563bc02
Refactor : early return instead of any()
sungjinwi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
기억할 키워드 | ||
dummy.next 리턴해서 건너뜀 | ||
list1 list2 둘 중 하나가 None이 되면 while문 끝내고 나머지 next로 이어줌 | ||
|
||
list1의 길이 M, list2의 길이N | ||
|
||
TC : O(M + N) | ||
|
||
SC : O(1) | ||
|
||
추가적인 풀이 : 알고달레에서 재귀방법 확인 | ||
""" | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
class Solution: | ||
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: | ||
dummy = ListNode() | ||
node = dummy | ||
|
||
while list1 and list2 : | ||
if list1.val < list2.val : | ||
node.next = list1 | ||
list1 = list1.next | ||
else : | ||
node.next = list2 | ||
list2 = list2.next | ||
node = node.next | ||
node.next = list1 or list2 | ||
return dummy.next |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
풀이 : 비지 않을경우 합 nSum을 구하고 list를 돌면서 빼고 남은 것이 답 | ||
|
||
|
||
TC : O(N) | ||
sum구할 떄 O(N) + for문 O(N) = O(2N)\ | ||
|
||
SC : O(1) | ||
""" | ||
|
||
class Solution: | ||
def missingNumber(self, nums: List[int]) -> int: | ||
nSum = sum(range(0,len(nums) + 1)) | ||
for num in nums : | ||
nSum -=num | ||
return nSum |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
풀이 : | ||
상하좌우 이동한 좌표가 board범위 벗어나면 False | ||
board[m][n]이 word[idx]와 불일치하면 False | ||
이미 방문했을경우 False | ||
단어가 완성되면 True | ||
상하좌우 한칸 이동한칸에 대해 재귀적 호출 | ||
상하좌우 중 True 있으면 True 없으면 False | ||
|
||
TC : O(M * N * 4 ^ W) | ||
board의 크기에 비례 -> M * N | ||
단어의 길이 만큼 상하좌우 재귀 호출 -> 4 ^ W | ||
|
||
SC : O(M * N + W) | ||
set의 메모리는 board 크기에 비례 -> M * N | ||
함수 호출 스택은 단어 길이에 비례 -> W | ||
""" | ||
|
||
class Solution: | ||
def exist(self, board: List[List[str]], word: str) -> bool: | ||
visit = set() | ||
|
||
def dfs(m: int, n: int, idx: int) -> bool: | ||
if not (0 <= m < row and 0 <= n < col): | ||
return False | ||
if not board[m][n] == word[idx]: | ||
return False | ||
if (m, n) in visit: | ||
return False | ||
if idx == len(word) - 1: | ||
return True | ||
|
||
visit.add((m, n)) | ||
for (r, c) in [(1, 0), (-1, 0), (0, 1), (0, -1)] : | ||
if(dfs(m + r, n + c, idx + 1)) : | ||
return True | ||
visit.remove((m, n)) | ||
return False | ||
|
||
row = len(board) | ||
col = len(board[0]) | ||
|
||
for m in range(row): | ||
for n in range(col): | ||
if dfs(m, n, 0): | ||
return True | ||
return False |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
안녕하세요 @sungjinwi 님
덕분에 if 문 없이
list1 or list2
으로 처리되는걸 알게 되었습니다.감사합니다. 😊