-
-
Notifications
You must be signed in to change notification settings - Fork 249
[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
Changes from 2 commits
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,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; | ||
} | ||
} |
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. brute force와 map 사용 모두 잘 해주셨네요! 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. 위 부분은 어떤식으로 하면 될까요??? BigO 표기법 사용이 맞나요?? 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. 넵넵 BigO 표기법 사용해 주시면 됩니다! |
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; | ||
} | ||
} |
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.
answer 사용의 목적이
nums.length != arr.size()
확인 용도라면 비교문 자체를 return해도 괜찮지 않을까요?return nums.length == arr.size();
같이요!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.
생각해보니 그렇네요...!!