-
Notifications
You must be signed in to change notification settings - Fork 12
Added Two Sum problem solution in Java #15
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
base: main
Are you sure you want to change the base?
Changes from all 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,15 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.*; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 1 in Solved-Problems/Two_Sum/Solution.java
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Solution { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public int[] twoSum(int[] nums, int target) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 4 in Solved-Problems/Two_Sum/Solution.java
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<Integer, Integer> map = new HashMap<>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 5 in Solved-Problems/Two_Sum/Solution.java
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < nums.length; i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 6 in Solved-Problems/Two_Sum/Solution.java
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int complement = target - nums[i]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 7 in Solved-Problems/Two_Sum/Solution.java
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (map.containsKey(complement)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 8 in Solved-Problems/Two_Sum/Solution.java
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new int[]{map.get(complement), i}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 9 in Solved-Problems/Two_Sum/Solution.java
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 10 in Solved-Problems/Two_Sum/Solution.java
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| map.put(nums[i], i); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 11 in Solved-Problems/Two_Sum/Solution.java
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 12 in Solved-Problems/Two_Sum/Solution.java
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new int[]{}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+3
to
+15
Contributor
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. Please format your code to pass the linting checks.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
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. Please revert the changes for this file. These changes will be updated automatically by the automation scripts when your PR is successfully merged. |
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.
It's a good practice to use only imports that are necessary.