Two Sum Problem on leetcode
Hi guys, I can't seem to figure out what the problem is here:
class Solution {
public int[] twoSum(int[] nums, int target) {
int result [] = {0, 0};
List<Integer> original = new ArrayList<Integer>();
List<Integer> complement = new ArrayList<Integer>();
for (int i = 0; i < nums.length; i++) {
original.add(nums[i]);
complement.add(target - nums[i]);
}
for (int i = 0; i < nums.length; i++) {
if (complement.contains(nums[i])) {
int second = complement.indexOf(nums[i]);
result [0] = i;
result [1] = second;
}
}
return result;
}
}
It does work for 62 out of 63 test cases but gives a wrong answer with the sets of [2, 4, 11, 3]. Just started learning programming, maybe I am missing something obvious? Thanks in advance.
class Solution {
public int[] twoSum(int[] nums, int target) {
int result [] = {0, 0};
List<Integer> original = new ArrayList<Integer>();
List<Integer> complement = new ArrayList<Integer>();
for (int i = 0; i < nums.length; i++) {
original.add(nums[i]);
complement.add(target - nums[i]);
}
for (int i = 0; i < nums.length; i++) {
if (complement.contains(nums[i])) {
int second = complement.indexOf(nums[i]);
result [0] = i;
result [1] = second;
}
}
return result;
}
}
It does work for 62 out of 63 test cases but gives a wrong answer with the sets of [2, 4, 11, 3]. Just started learning programming, maybe I am missing something obvious? Thanks in advance.