-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #752 from jinah92/main
[jinah92] Week 2
- Loading branch information
Showing
3 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,20 @@ | ||
# 공간복잡도 : O(1), 시간복잡도 : O(N^2) | ||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
three_sums = set() | ||
nums.sort() | ||
|
||
for i in range(len(nums)-2): | ||
low, high = i + 1, len(nums)-1 | ||
while low < high: | ||
three_sum = nums[i] + nums[high] + nums[low] | ||
if three_sum < 0: | ||
low += 1 | ||
elif three_sum > 0: | ||
high -= 1 | ||
else: | ||
three_sums.add((nums[i], nums[high], nums[low])) | ||
low, high = low+1, high-1 | ||
|
||
return list(three_sums) | ||
|
This file contains 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,11 @@ | ||
# 공간복잡도: O(1), 시간복잡도: O(n) | ||
class Solution: | ||
def climbStairs(self, n: int) -> int: | ||
if n < 3: | ||
return n | ||
|
||
prev, curr = 1, 2 | ||
for num in range(3, n+1): | ||
prev, curr = curr, prev + curr | ||
|
||
return curr |
This file contains 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,14 @@ | ||
# 공간복잡도: O(n), 시간복잡도: O(n) | ||
class Solution: | ||
def isAnagram(self, s: str, t: str) -> bool: | ||
char_set_1, char_set_2 = {}, {} | ||
|
||
for ch in s: | ||
char_set_1[ch] = 0 if ch not in char_set_1 else char_set_1[ch] + 1 | ||
|
||
for ch in t: | ||
char_set_2[ch] = 0 if ch not in char_set_2 else char_set_2[ch] + 1 | ||
|
||
# dictionary의 모든 요소 종류와 개수가 일치해야 함 | ||
return char_set_1 == char_set_2 | ||
|