Java sum algorithm not returning anything

时间:2017-08-05 11:49:24

标签: java algorithm sum

I'm trying to solve the two sum algorithm on Leetcode:

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice.

And came up with this:

public class Leet {

    public static void main(String[] args) {
        System.out.println(Arrays.toString(twoSum(new int[]{1, 2, 3, 4, 5, 6}, 2)));
    }

    public static int[] twoSum(int[] nums, int target) {
        int[] answer = null;
        int i = 0;
        for (int j = nums[i]; i < nums.length; i++) {
            for (int x : nums) {
                if (x != j & (j + x) == target) {
                    int x2 = java.util.Arrays.asList(nums).indexOf(x);
                    answer[0] = i;
                    answer[1] = x2;
                } else {
                    return nums;
                }

            }

        }
        System.out.println("leet method executed");

        return answer;
    }
}

The problem is it's not returning anything, not event the printed statement. Any ideas?

3 个答案:

答案 0 :(得分:1)

I can see 2 things wrong or not intended in the program (leaving aside whether it's the best approach to the problem).

  1. As stated in a comment, you should use && instead of & for boolean AND. & is bitwise AND, useful for integer bit twiddling.
  2. You declare answer as an array, but never create space for it. You need to say int [] answer = new int[2]; or similar.

I haven't run the code, but if the test program ends with no output, check that you aren't getting a NullPointerException (caused by #2 above).

答案 1 :(得分:1)

Don't forget about null-checks and the case where no correct number-pair was found.

public static int[] twoSum(int[] numbers, int target) {
    if(Objects.isNull(numbers)){
        throw new IllegalArgumentException("numbers is not allowed to be null");
    }

    for (int a=0;a<numbers.length;a++) {
        int first = numbers[a];

        for (int b=0;b<numbers.length;b++) {
            int second = numbers[b];

            if (first + second == target && a!=b) {
                return new int[] { first, second };
            }
        }
    }
    throw new IllegalArgumentException("there has to be a matching pair");
}

答案 2 :(得分:1)

See some fixes. Remember about array initialization and cases when values can be same in the array.

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] answer = null;

        for(int i=0;i<nums.length;i++) {
            int value = nums[i];

            for(int j=i+1;j<nums.length;j++) {
                int x=nums[j];
                if ((value+x)==target) {
                    answer = new int[2]; 
                    answer[0]=i;
                    answer[1]=j; 
                }
            }
        }

        System.out.println("leet method executed");

        if (answer == null) {
            return nums;
        }

        return answer;
    }
}