-
-
Notifications
You must be signed in to change notification settings - Fork 248
[sm9171] Week01 Solutions #1158
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
Changes from 3 commits
894505b
c53e52c
e68bd88
918bba5
b249de9
115c0e3
23984e6
6df5759
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
HashSet<Integer> set = new HashSet<>(); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
set.add(nums[i]); | ||
} | ||
|
||
if (nums.length == set.size()) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Solution { | ||
public int[] topKFrequent(int[] nums, int k) { | ||
Map<Integer, Integer> hashMap = new HashMap<>(); | ||
for (int i = 0; i < nums.length; i++) { | ||
int num = nums[i]; | ||
if (hashMap.containsKey(num)) { | ||
hashMap.put(num, hashMap.get(num) + 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getOrDefault를 쓰니 한줄로 줄일 수 있겠네요! |
||
continue; | ||
} | ||
hashMap.put(num, 1); | ||
} | ||
|
||
List<Integer> list = hashMap.entrySet() | ||
.stream() | ||
.sorted(Map.Entry.<Integer, Integer>comparingByValue().reversed()) | ||
.limit(k) | ||
.map(Map.Entry::getKey) | ||
.toList(); | ||
|
||
int [] result = new int[list.size()]; | ||
for (int i = 0; i < list.size(); i++) { | ||
result[i] = list.get(i); | ||
} | ||
|
||
return result; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위에 List list 부분에서 stream을 사용하신 것 처럼, 반환값도 stream을 활용해보시는 건 어떨까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 array로 타입 변경하는 부분도 스트림을 이용하여 해결해 보았습니다 :) |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
public int[] twoSum(int[] nums, int target) { | ||
Map<Integer, Integer> hashMap = new HashMap<>(); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
int gap = target - nums[i]; | ||
if(hashMap.containsKey(gap)){ | ||
int j = hashMap.get(gap); | ||
return new int[]{i,j}; | ||
} | ||
hashMap.put(nums[i], i); | ||
} | ||
return null; | ||
} | ||
} |
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.
add 할 때 존재하는지 확인하고 바로 return 하는 건 어떨까요?
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.
wisdon08님이 좋은 의견 남겨주신것에 저도 동의합니다!
현재는 배열을 끝까지 순회한 후에 중복을 판단하는 구조인것 같아서, 중복을 미리 찾고 빠르게 결과 반환을 하는 구조로 변경하는 것도 좋아 보입니다 😀
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.
두 분 의견 감사합니다. 얘기 해주신대로 얼리 리턴을 하니 17ms에서 12ms로 단축 되었네요 :)