two sum java

Solutions on MaxInterview for two sum java by the best coders in the world

showing results for - "two sum java"
Lucien
06 Aug 2018
1class Solution {
2    public int[] twoSum(int[] nums, int target) {
3        int[] sol = new int[2];
4        if(nums.length == 2) {
5            sol[0] = 0;
6            sol[1] = 1;
7        }
8        else {
9            for(int i = 0; i < nums.length-1; i++) {
10                for(int j = 1; j <= nums.length-1; j++) {
11                    if((nums[i] + nums[j]) == target && i != j) {
12                        sol[0] = i;
13                        sol[1] = j;
14                        break;
15                    }
16                }
17            }
18        }
19        return sol;
20    }
21}