-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
58 lines (39 loc) · 1.34 KB
/
Copy pathTwoSum.java
File metadata and controls
58 lines (39 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.LeetCode;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class TwoSum {
/*
LeetCode Algorithm Question - Two Sum Solution
Solved Date : 2020-10-31
Author : TK Lee
Source : https://leetcode.com/problems/two-sum/
Question Description :
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
*/
public static int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> numbers = new HashMap<Integer, Integer>();
int[] sum = {0, 0};
int n =0;
for (int idx=0; idx<nums.length; idx++){
n=target-nums[idx];
if (numbers.containsKey(n)){
sum[0] = idx;
sum[1] = numbers.get(n);
} else {
numbers.put(nums[idx], idx);
}
}
Arrays.sort(sum);
return sum;
}
public static void main (String[] args) {
int[] numSet1 = {3,2,4};
int[] result = new int [2];
result=twoSum(numSet1, 6);
System.out.println (result[0]+ ", " + result[1]);
}
}