-
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 #749 from paragon0107/main
[Paragon0107] Week2
- Loading branch information
Showing
3 changed files
with
65 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,28 @@ | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
class Solution { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
Set<List<Integer>> set = new HashSet<>(); | ||
Arrays.sort(nums); | ||
for (int i = 0; i < nums.length-2; i++) { | ||
int start = i + 1; | ||
int end = nums.length - 1; | ||
while (start < end) { | ||
int sum = nums[i] + nums[start] + nums[end]; | ||
if (sum < 0 ) { | ||
start++; | ||
} else if (sum > 0) { | ||
end--; | ||
}else { | ||
set.add(Arrays.asList(nums[i], nums[start], nums[end])); | ||
start++; | ||
end--; | ||
} | ||
} | ||
} | ||
return set.stream().toList(); | ||
} | ||
} |
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,19 @@ | ||
/* | ||
* | ||
* 시간 복잡도: | ||
* 바텀업 형식으로 배열을 훑으며 올라가기 때문에 O(N) | ||
* 공간 복잡도: | ||
* 자연수 마다 해당하는 방법의 갯수를 저장하기 때문에 O(N) | ||
* | ||
* */ | ||
class Solution { | ||
public int climbStairs(int n) { | ||
int[] dp = new int[n + 1]; | ||
dp[1] = 1; | ||
dp[2] = 2; | ||
for(int i=3;i<=n;i++){ | ||
dp[i] = dp[i - 2] + dp[i - 1]; | ||
} | ||
return dp[n]; | ||
} | ||
} |
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,18 @@ | ||
import java.util.Arrays; | ||
/* | ||
* 시간 복잡도: | ||
* toCharArray는 O(1)의 복잡도를 갖고 ArraySor의 경우 평균O(nlogn), 최악O(n^2)를 갖음(코테시 몇으로 계산하고 진행해야 할 지는 잘 모르겠네요..) | ||
* 공간 복잡도: | ||
* s와t를 사용해서 그대로 배열로 만들기 때문에 O(n) | ||
* | ||
* | ||
* */ | ||
class Solution { | ||
public static boolean isAnagram(String s, String t) { | ||
char[] s1 = s.toCharArray(); | ||
char[] s2 = t.toCharArray(); | ||
Arrays.sort(s1); | ||
Arrays.sort(s2); | ||
return Arrays.equals(s1, s2); | ||
} | ||
} |