-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Tessa1217] Week 02 solutions #1212
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,63 @@ | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
// 정수 배열 nums가 주어질 때 nums[i], nums[j], nums[k]로 이루어진 배열을 반환하시오 | ||
// 반환 배열 조건: i 가 j와 같지 않고, i가 k와 같지 않으며 세 요소의 합이 0인 배열 | ||
class Solution { | ||
|
||
// 시간복잡도: O(n^2) | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
|
||
Arrays.sort(nums); | ||
|
||
List<List<Integer>> answer = new ArrayList<>(); | ||
|
||
int left = 0; | ||
int right = 0; | ||
int sum = 0; | ||
|
||
for (int i = 0; i < nums.length - 2 && nums[i] <= 0; i++) { | ||
|
||
// 중복 제거 | ||
if (i > 0 && nums[i] == nums[i - 1]) { | ||
continue; | ||
} | ||
|
||
left = i + 1; | ||
right = nums.length - 1; | ||
|
||
while (left < right) { | ||
|
||
sum = nums[i] + nums[left] + nums[right]; | ||
// System.out.println(String.format("%d, %d, %d", i, left, right)); | ||
// System.out.println(String.format("%d + %d + %d = %d", nums[i], nums[left], nums[right], sum)); | ||
if (sum < 0) { | ||
left++; | ||
continue; | ||
} | ||
if (sum > 0) { | ||
right--; | ||
continue; | ||
} | ||
|
||
answer.add(List.of(nums[i], nums[left], nums[right])); | ||
|
||
// 중복 제거 | ||
while (left < right && left + 1 < nums.length && nums[left] == nums[left + 1]) { | ||
left++; | ||
} | ||
while (left < right && right - 1 >= 0 && nums[right] == nums[right - 1]) { | ||
right--; | ||
} | ||
left++; | ||
right--; | ||
|
||
} | ||
} | ||
|
||
return answer; | ||
} | ||
|
||
} | ||
|
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,26 @@ | ||
/** | ||
당신은 계단을 오르고 있다. 정상에 오르기까지 n 계단을 올라야 한다. | ||
계단을 오를 때마다 1 계단 또는 2 계단씩 오를 수 있을 때 정상에 도달하기까지의 경우의 수를 구하시오. | ||
*/ | ||
|
||
public class Solution { | ||
|
||
// 시간복잡도: O(n) | ||
public int climbStairs(int n) { | ||
|
||
if (n == 1 || n == 2) { | ||
return n; | ||
} | ||
|
||
int[] cases = new int[n + 1]; | ||
cases[1] = 1; | ||
cases[2] = 2; | ||
for (int i = 3; i <= n; i++) { | ||
cases[i] = cases[i - 1] + cases[i - 2]; | ||
} | ||
|
||
return cases[n]; | ||
} | ||
|
||
} | ||
|
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,26 @@ | ||
/** | ||
정수 배열 nums가 주어질 때 answer[i]가 nums[i]를 제외한 나머지 요소의 곱인 answer 배열을 반환하시오. | ||
*/ | ||
public class Solution { | ||
|
||
/** 시간복잡도 O(n) */ | ||
public int[] productExceptSelf(int[] nums) { | ||
|
||
int[] answer = new int[nums.length]; | ||
|
||
int start = 1; | ||
for (int i = 0; i < nums.length; i++) { | ||
answer[i] = start; | ||
start *= nums[i]; | ||
} | ||
|
||
int end = 1; | ||
for (int i = nums.length - 1; i >= 0; i--) { | ||
answer[i] *= end; | ||
end *= nums[i]; | ||
} | ||
|
||
return answer; | ||
} | ||
} | ||
|
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,60 @@ | ||
/** | ||
두 문자열 s와 t가 주어질 때 t가 s의 애너그램이라면 true, 아니면 false를 반환하세요. | ||
*/ | ||
|
||
import java.util.Map; | ||
import java.util.HashMap; | ||
import java.util.Arrays; | ||
public class Solution { | ||
|
||
// 2차 풀이 (맵 활용하여 시간 복잡도 줄이기, 시간복잡도: O(n)) | ||
public boolean isAnagram(String s, String t) { | ||
|
||
Map<Character, Integer> charMap = new HashMap<>(); | ||
|
||
char[] sArr = s.toCharArray(); | ||
for (char sa : sArr) { | ||
charMap.put(sa, charMap.getOrDefault(sa, 0) + 1); | ||
} | ||
|
||
char[] tArr = t.toCharArray(); | ||
for (char ta : tArr) { | ||
charMap.put(ta, charMap.getOrDefault(ta, 0) - 1); | ||
} | ||
|
||
for (int cnt : charMap.values()) { | ||
if (cnt != 0) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
|
||
} | ||
|
||
|
||
// 1차 풀이 (정렬로 인해 O(n log n)) | ||
// public boolean isAnagram(String s, String t) { | ||
// | ||
// if (s.length() != t.length()) { | ||
// return false; | ||
// } | ||
// | ||
// char[] sArr = s.toCharArray(); | ||
// char[] tArr = t.toCharArray(); | ||
// | ||
// Arrays.sort(sArr); | ||
// Arrays.sort(tArr); | ||
// | ||
// for (int i = 0; i < sArr.length; i++) { | ||
// if (sArr[i] != tArr[i]) { | ||
// return false; | ||
// } | ||
// } | ||
// | ||
// return true; | ||
// | ||
// } | ||
|
||
} | ||
|
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,36 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
/** | ||
이진 트리의 root가 주어졌을 때 유효한 이진 탐색 트리인지 검증하세요. | ||
*/ | ||
class Solution { | ||
|
||
/** 시간복잡도 O(n) */ | ||
public boolean isValidBST(TreeNode root) { | ||
return isValidRange(root, Long.MIN_VALUE, Long.MAX_VALUE); | ||
} | ||
|
||
private boolean isValidRange(TreeNode root, long min, long max) { | ||
if (root == null) { | ||
return true; | ||
} | ||
if (root.val <= min || root.val >= max) { | ||
return false; | ||
} | ||
return isValidRange(root.left, min, root.val) && isValidRange(root.right, root.val, max); | ||
} | ||
} | ||
|
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.
훌륭한 아이디어십니다!