Skip to content

Week3 #776

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 3 commits into from
Dec 29, 2024
Merged

Week3 #776

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
21 changes: 21 additions & 0 deletions two-sum/paragon0107.java
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. nums를 두번 순회하고 있는데, 순회를 한번으로 줄일 수 있을까요?
  2. Big O 분석까지 해주시면 더 좋을 것 같아요 ㅎㅎ

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

import java.util.HashMap;
import java.util.Map;

class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for(int i =0;i<nums.length;i++){
map.put(nums[i], i);
}

for(int i = 0;i<nums.length;i++){
int b = target - nums[i];
if(map.containsKey(b) && map.get(b) != i){
int j = map.get(b);
return new int[]{i,j};
}
}
return null;
}
}
Loading