-
-
Notifications
You must be signed in to change notification settings - Fork 304
[HYUNAHKO] WEEK 02 solutions #2102
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
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3229bea
Week02 Solutions
HYUNAHKO 38d4c3c
Week 03 soultions
HYUNAHKO d5ccec9
end enter line
HYUNAHKO c6719d1
end enter line
HYUNAHKO c3bc51b
WEEK 03 Soultions
HYUNAHKO ee42edc
Enter end line
HYUNAHKO 1257307
Update number-of-1-bits/HYUNAHKO.py
HYUNAHKO f7bc00e
Update valid-palindrome/HYUNAHKO.py
HYUNAHKO 0f7addd
Final Revisions
HYUNAHKO 5d1acd7
Merge branch 'main' of https://github.com/HYUNAHKO/leetcode-study
HYUNAHKO 0e1c3eb
Final resolve merge conflict
HYUNAHKO 62dad6b
resolve merge conflict
HYUNAHKO b6436f7
Real Final resolve merge conflict
HYUNAHKO 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
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,27 @@ | ||
| class Solution: | ||
| def climbStairs(self, n: int) -> int: | ||
| if (n<1 or n>45): | ||
| return 0 | ||
|
|
||
| def factorial(num): | ||
| if num <= 1: | ||
| return 1 | ||
|
|
||
| result = 1 | ||
| for i in range(2, num+1): | ||
| result *= i | ||
| return result | ||
|
|
||
| steps = 0 | ||
| cur_steps = 1 | ||
| quotient = n // 2 | ||
| k=0 | ||
|
|
||
| for _ in range(quotient+1): | ||
| cur_steps = factorial(n-k) / (factorial(k)*factorial(n-2*k)) | ||
| k+=1 | ||
| steps += cur_steps | ||
|
|
||
| return int(steps) | ||
|
|
||
|
|
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,25 @@ | ||
| class Solution: | ||
| def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
| result = [] | ||
|
|
||
| def backtrack(start_idx, current_combination, current_sum): | ||
| if current_sum == target: | ||
| result.append(current_combination[:]) | ||
| return | ||
| if current_sum > target: | ||
| return | ||
|
|
||
| # 모든 후보 탐색 | ||
| for i in range(start_idx, len(candidates)): | ||
| # 현재 숫자 선택 | ||
| current_combination.append(candidates[i]) | ||
|
|
||
| # 같은 숫자를 다시 사용할 수 있으므로 i부터 시작 | ||
| backtrack(i, current_combination, current_sum + candidates[i]) | ||
|
|
||
| # 백트래킹 | ||
| current_combination.pop() | ||
|
|
||
| backtrack(0, [], 0) | ||
| return result | ||
|
|
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,10 @@ | ||
| class Solution: | ||
| def hammingWeight(self, n: int) -> int: | ||
| result = 1 | ||
| while (n//2 != 0): | ||
| remainder = n % 2 | ||
| if (remainder==1): | ||
| result+=1 | ||
| n = n//2 | ||
| return result | ||
|
|
||
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,41 @@ | ||||
| class Solution: | ||||
| def productExceptSelf(self, nums: List[int]) -> List[int]: | ||||
| n = len(nums) | ||||
| result_list = [0] * n | ||||
| if len(nums) <2 or len(nums) > 1e5: | ||||
| return None | ||||
|
|
||||
| p = 1 | ||||
| for i in range(n): | ||||
| result_list[i] = p | ||||
| p *= nums[i] | ||||
|
|
||||
| p = 1 | ||||
| for i in range(n - 1, -1, -1): | ||||
| result_list[i] *= p | ||||
| p *= nums[i] | ||||
|
|
||||
| return result_list | ||||
|
|
||||
|
|
||||
|
|
||||
| class Solution: | ||||
| def productExceptSelf(self, nums: List[int]) -> List[int]: | ||||
| result_list = [0] * len(nums) | ||||
| if len(nums) <2 or len(nums) > 1e5: | ||||
| return None | ||||
|
|
||||
| for idx in range(0, len(nums)): | ||||
| result = 1 | ||||
| for idx_left in range(0, idx): | ||||
| result *= nums[idx_left] | ||||
|
|
||||
| for idx_right in range(idx+1, len(nums)): | ||||
| result *= nums[idx_right] | ||||
|
|
||||
| result_list[idx] = result | ||||
|
|
||||
|
|
||||
| return result_list | ||||
|
|
||||
|
|
||||
|
||||
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,25 @@ | ||
| from collections import Counter | ||
|
|
||
| class Solution: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| letter_dict1 = {} | ||
| letter_dict2 = {} | ||
| letter_list1 = list(s) | ||
| letter_list2 = list(t) | ||
|
|
||
| for i in letter_list1: | ||
| letter_dict1[i] = letter_dict1.get(i, 0) + 1 | ||
|
|
||
| for j in letter_list2: | ||
| letter_dict2[j] = letter_dict2.get(j, 0) + 1 | ||
|
|
||
| if (letter_dict1 == letter_dict2): | ||
| return True | ||
| else: | ||
| return False | ||
|
|
||
|
|
||
| class Solution: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| return Counter(s) == Counter(t) | ||
|
|
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,13 @@ | ||
| class Solution: | ||
| def isPalindrome(self, s: str) -> bool: | ||
| s = s.lower() | ||
| result = [char for char in s if char.isalnum()] | ||
|
|
||
| n = len(result) | ||
| for i in range(n): | ||
| str1 = result[i] | ||
| if (str1 != result[n-i-1]): | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
HYUNAHKO marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.