在数组中添加两个数字,其和必须等于目标值。

huangapple go评论59阅读模式
英文:

add twoNumber in the array and must equal target

问题

public int[] twoSum(int[] nums, int target) {
    for (int i = 0; i < nums.length - 1; i++) {
        for (int j = i + 1; j < nums.length; j++) {
            if (nums[i] + nums[j] == target) {
                int[] result = {i, j};
                return result;
            }
        }
    }
    return null;
}
英文:

I have this program where the number of elements of the array "n" is entered and then the target is entered "d",
Then I want to add two numbers from the matrix and the addition is equal to target "d",
Then I want to return an array of Indexes of these two numbers
Example:

 Input: nums = [2,7,11,15], target = 9
 Output: [0,1]
 Output: Because nums[0] + nums[1] == 9, we return [0, 1].

I wrote this program, but I could not return the Indexes,
How can I solve the problem?

public int[] twoSum(int[] nums, int target) {
    for (int i = 0; i &lt; nums.length - 1; i++) {
        for (int j = i + 1; j &lt; nums.length; j++) {
            if (nums[i] + nums[j] == target) {
                System.out.println(&quot;This the number that we want it&quot; + &quot;\n&quot; + i + &quot;\n&quot; + j);
            }
        }
    }
    return nums;
}

答案1

得分: 2

public int[] twoSum(int[] nums, int target) {
    int[] result = new int[2];

    for (int i = 0; i < nums.length - 1; i++) {
        for (int j = i + 1; j < nums.length; j++) {
            if (nums[i] + nums[j] == target) {
                result[0] = i;
                result[1] = j;
                System.out.println("这是我们要找的数" + "\n" + i + "\n" + j);
            }
        }
    }

    return result;
}

如果您对O(n^2)的复杂度满意,您可以不必继续阅读。但这里是一个O(n)时间复杂度的解决方案:

public int[] twoSum(int[] numbers, int target) {
    Map<Integer, Integer> map = new HashMap<>();

    for (int i = 0; i < numbers.length; map.put(numbers[i], ++i)) {
        if (map.containsKey(target - numbers[i])) {
            return new int[] {map.get(target - numbers[i]), i + 1};
        }
    }

    return new int[]{0,0};
}
英文:
public int[] twoSum(int[] nums, int target) {

    int[] result = new int[2];

    for (int i = 0; i &lt; nums.length - 1; i++) {
        for (int j = i + 1; j &lt; nums.length; j++) {
            if (nums[i] + nums[j] == target) {
                res[0] = i;
                res[1] = j;
                System.out.println(&quot;This the number that we want it&quot; + &quot;\n&quot; + i + &quot;\n&quot; + j);
            }
        }
    }

    return res;
}

You need to create a new array of size 2 and return that.


If you are happy with O(n^2) complexity, you don't need to read ahead. But here is a solution with O(n) time complexity:

public int[] twoSum(int[] numbers, int target) {
    Map&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();

    for (int i = 0; i &lt; numbers.length; map.put(numbers[i], ++i)) {
        if (map.containsKey(target - numbers[i])) {
            return new int[] {map.get(target - numbers[i]), i + 1};
        }
    }

    return new int[]{0,0};
}

答案2

得分: 1

看看这些解决方案!

时间复杂度为 O(n^2),空间复杂度为 O(1)

public static int[] foo(int[] arr, int d) {
    for (int i = 0; i < arr.length - 1; i++)
        for (int j = i + 1; j < arr.length; j++)
            if (arr[i] + arr[j] == d)
                return new int[] { i, j };

    return new int[] {-1, -1};
}

时间复杂度为 O(n),空间复杂度为 O(n)

public static int[] foo(int[] arr, int d) {
    Map<Integer, Integer> map = new HashMap<>();

    for (int i = 0; i < arr.length; i++) {
        if (map.containsKey(d - arr[i]))
            return new int[] { map.get(d - arr[i]), i };

        map.put(arr[i], i);
    }

    return new int[] {-1, -1};
}
英文:

Look at this solutions!

Time complexity O(n^2), space complexity O(1)

public static int[] foo(int[] arr, int d) {
    for (int i = 0; i &lt; arr.length - 1; i++)
        for (int j = i + 1; j &lt; arr.length; j++)
            if (arr[i] + arr[j] == d)
                return new int[] { i, j };

    return new int[] {-1, -1};
}

Time complexity O(n), space complexity O(n)

public static int[] foo(int[] arr, int d) {
    Map&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();

    for (int i = 0; i &lt; arr.length; i++) {
        if (map.containsKey(d - arr[i]))
            return new int[] { map.get(d - arr[i]), i };

        map.put(arr[i], i);
    }

    return new int[] {-1, -1};
}

huangapple
  • 本文由 发表于 2020年9月28日 02:10:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64091738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定