-
-
Notifications
You must be signed in to change notification settings - Fork 195
[yooncheoloh] Week1 #645
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
[yooncheoloh] Week1 #645
Changes from all commits
Commits
Show all changes
2 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,16 @@ | ||
class Solution { | ||
/** | ||
* 시간 복잡도: O(N) | ||
* 공간 복잡도: O(N) | ||
*/ | ||
public boolean containsDuplicate(int[] nums) { | ||
Set<Integer> set = new HashSet<>(); | ||
|
||
for (int num : nums) { | ||
if (set.contains(num)) return true; | ||
set.add(num); | ||
} | ||
|
||
return false; | ||
} | ||
} |
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,19 @@ | ||
class Solution { | ||
public int rob(int[] nums) { | ||
//배열 길이 0이면 털 수 있는 집이 없음. | ||
if (nums.length == 0) return 0; | ||
//배열 길이가 1이면 한 집만 털 수 있음. | ||
if (nums.length == 1) return nums[0]; | ||
|
||
//동적 계획법으로 풀이 | ||
int[] dp = new int[nums.length]; | ||
dp[0] = nums[0]; | ||
dp[1] = Math.max(nums[0], nums[1]); | ||
|
||
//배열 크기가 2이상일 경우 최대 금액의 범위 확장 | ||
for (int i = 2; i < nums.length; i++) { | ||
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); | ||
} | ||
return dp[nums.length - 1]; | ||
} | ||
} |
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,34 @@ | ||
import java.util.*; | ||
|
||
class Solution { | ||
public int longestConsecutive(int[] nums) { | ||
if (nums == null || nums.length == 0) return 0; | ||
|
||
//모든 요소 HashSet 삽입 | ||
HashSet<Integer> set = new HashSet<>(); | ||
for (int num : nums) { | ||
set.add(num); | ||
} | ||
|
||
int longest = 0; | ||
|
||
// 시작지점 체크 | ||
for (int num : nums) { | ||
//배열 요소보다 1 작은 수 가 없는 경우 새로운 시작 지점이 됨 | ||
if (!set.contains(num - 1)) { | ||
int start = num; | ||
int currentLength = 1; | ||
|
||
// 1씩 증가시키면서 연속된 수의 개수 탐색 | ||
while (set.contains(start + 1)) { | ||
start++; | ||
currentLength++; | ||
} | ||
// 기존 longest와 현재 연속된 수를 비교 | ||
longest = Math.max(longest, currentLength); | ||
} | ||
} | ||
|
||
return longest; | ||
} | ||
} |
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,21 @@ | ||
import java.util.*; | ||
|
||
class Solution { | ||
public int[] topKFrequent(int[] nums, int k) { | ||
Map<Integer, Integer> map = new HashMap<>(); | ||
for (int num : nums) { | ||
map.put(num, map.getOrDefault(num, 0) + 1); | ||
} | ||
|
||
//value값을 통한 key 정렬 | ||
List<Integer> list = new ArrayList<>(map.keySet()); | ||
list.sort((a,b)->map.get(b) - map.get(a)); | ||
|
||
//상위 k개 key 추출 | ||
int[] res = new int[k]; | ||
for (int i = 0; i < k; i++) { | ||
res[i] = list.get(i); | ||
} | ||
return res; | ||
} | ||
} |
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 @@ | ||
class Solution { | ||
public boolean isPalindrome(String s) { | ||
//정규식으로 영문,숫자 아닌 경우 "" 치환 | ||
//StringBuilder를 통해 문자열 가공 | ||
//거꾸로 뒤집은 것과 원래의 문자열이 일치할 경우 팰린드롬 | ||
s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); | ||
StringBuilder sb = new StringBuilder(s); | ||
if(sb.reverse().toString().equals(s)){ | ||
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.
역시 문자열 패턴매칭은 정규식이 깔끔한 것 같습니다