Skip to content

[leebeanbin] WEEK 01 Solution #1674

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions contains-duplicate/leebeanbin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.HashSet;

class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> arr = new HashSet<Integer>();
boolean answer = false;

for(int num : nums){
arr.add(num);
}

if(nums.length != arr.size()){
answer = true;
}

return answer;
Comment on lines +12 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

answer 사용의 목적이 nums.length != arr.size() 확인 용도라면 비교문 자체를 return해도 괜찮지 않을까요? return nums.length == arr.size(); 같이요!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생각해보니 그렇네요...!!

}
}
16 changes: 16 additions & 0 deletions top-k-frequent-elements/leebeanbin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.HashMap;
import java.util.Map;

public class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();

// count how many duplicated numbers in the input array
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}

return map.entrySet().stream().sorted((a, b) -> b.getValue() - a.getValue()).limit(k)
.mapToInt(Map.Entry::getKey).toArray();
}
}
28 changes: 28 additions & 0 deletions two-sum/leebeanbin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.HashMap;

public class leebeanbin {
public static int[] bruteForce(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
return new int[]{i, j};
}
}
}
return null;
}

public static int[] hashMap(int[] nums, int target) {
HashMap<Integer, Integer> arr = new HashMap<>();

for (int i = 0; i < nums.length; i++) {
arr.put(nums[i], i);

if (arr.containsKey(target - nums[i])) {
return new int[]{arr.get(target - nums[i]), i};
}
}

return null;
}
}