簡體   English   中英

來自 leetcode 的 Java 中的兩個和

[英]Two sums in Java from leetcode

我正在處理 leetcode 問題。 我剛剛解決了以下問題:

給定一個整數數組,找到兩個數字,使它們加起來等於一個特定的目標數字。

function twoSum 應該返回兩個數字的索引,以便它們相加到目標,其中 index1 必須小於 index2。 請注意,您返回的答案(index1 和 index2)不是從零開始的。

您可以假設每個輸入都只有一個解決方案。

輸入:數字={2, 7, 11, 15},目標=9 Output:index1=1,index2=2

我的代碼在這里:

public class Solution {
    public int[] twoSum(int[] numbers, int target) {
        
            int len = numbers.length;

        int[] result = new int[2] ;
        int number1 = 0;
        int sum = 0;

        for (int i = 0; i < len; i++) {
            number1 = numbers[i];

            for(int j = i+1; j < len; j++)
            {

                sum = number1+numbers[j];       
                if(sum == target)
                {
                    result[0]=i;
                    result[1]=j;
                }
            }
        
        }
        return result;
    }
}

它給了我一個超出時間限制; 但是,我認為這個解決方案可能有效。 誰能告訴我這個解決方案是否足夠好?

那是因為您的解決方案具有 O(n*n) 時間復雜度。 如果使用 Map,這個問題可以通過 O(n) 時間復雜度來解決

public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> map = new HashMap<>();
        for(int i =0; i< nums.length; i++) {
            if(map.containsKey(target - nums[i])){
                return new int[]{map.get(target - nums[i]), i};
            }else {
                map.put(nums[i], i);
            }
        }
        return new int[]{-1,-1};
    }

這樣可以減少計時問題,但我不能保證它將完全解決問題!

您可以在找到解決方案( result )后立即return ,因為在問題中指出,對於給定問題恰好存在一個解決方案,即

if(sum == target)
{
    result[0]=i;
    result[1]=j;
    return result;
}

而不是繼續搜索其余輸入。

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

    for(int x = 0; x < nums.length ; x++){
        for(int y = 0; y < nums.length ; y++){
            if(x != y && nums[x] + nums[y] == target){
            indices[0] = x;
            indices[1] = y;
            return indices;
        }
        }
    }
    return indices;
}

}

這是一個leetcode 問題

當然有一個有效的解決方案。 時間復雜度為 O(n)

class Solution {
    public int[] twoSum(int[] nums, int target) {

        HashMap<Integer, Integer> map = new HashMap();
        
        /* 2, 7, 11, 15  ==  target=9
         * 0  1   2   3  => 4
         *  
         *    --HasMap--
         *  key   value(index)
         *   2  ->  0
         *   7  ->  1
         *
        */
        for(int i=0; i<nums.length; i++) {
           int expectedKey = target - nums[i];
           if(map.containsKey(expectedKey)) {
               return new int[]{map.get(expectedKey), i};
           }
           map.put(nums[i], i);
        }
             
        throw new IllegalArgumentException("No Solution");
    }
}
class Solution {
public int[] twoSum(int[] nums, int target) {
    int[] a = new int[2];
    for (int i = 0; i < nums.length; i++) {
        for (int j = i + 1; j < nums.length; j++) {
            if (nums[i] + nums[j] == target) {
                a[0] = i;
                a[1] = j;
            }
        }
    }
    return a;
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM