Answers for "two sum java"

0

two sum java

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] sol = new int[2];
        if(nums.length == 2) {
            sol[0] = 0;
            sol[1] = 1;
        }
        else {
            for(int i = 0; i < nums.length-1; i++) {
                for(int j = 1; j <= nums.length-1; j++) {
                    if((nums[i] + nums[j]) == target && i != j) {
                        sol[0] = i;
                        sol[1] = j;
                        break;
                    }
                }
            }
        }
        return sol;
    }
}
Posted by: Guest on March-12-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language