-
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?
Added Two Sum problem solution in Java #15
Conversation
Chintha-vardhan
commented
Oct 12, 2025
- Added folder Two_Sum with Solution.java
- Updated index.md with problem link
|
👋 @Chintha-vardhan |
iamwatchdogs
left a comment
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.
Please make the suggested changes to approve the PR.
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.
Please revert the changes for this file. These changes will be updated automatically by the automation scripts when your PR is successfully merged.
| @@ -0,0 +1,15 @@ | |||
| import java.util.*; | |||
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.
| import java.util.*; | |
| 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++) { | ||
| int complement = target - nums[i]; | ||
| if (map.containsKey(complement)) { | ||
| return new int[]{map.get(complement), i}; | ||
| } | ||
| map.put(nums[i], i); | ||
| } | ||
| return new int[]{}; | ||
| } | ||
| } |
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.
Please format your code to pass the linting checks.
| class Solution { | |
| public int[] twoSum(int[] nums, int target) { | |
| Map<Integer, Integer> map = new HashMap<>(); | |
| for (int i = 0; i < nums.length; i++) { | |
| int complement = target - nums[i]; | |
| if (map.containsKey(complement)) { | |
| return new int[]{map.get(complement), i}; | |
| } | |
| map.put(nums[i], i); | |
| } | |
| return new int[]{}; | |
| } | |
| } | |
| class Solution { | |
| public int[] twoSum(int[] nums, int target) { | |
| Map<Integer, Integer> map = new HashMap<>(); | |
| for (int i = 0; i < nums.length; i++) { | |
| int complement = target - nums[i]; | |
| if (map.containsKey(complement)) { | |
| return new int[] {map.get(complement), i}; | |
| } | |
| map.put(nums[i], i); | |
| } | |
| return new int[] {}; | |
| } | |
| } |