【问题标题】:Two sums in Java from leetcode来自 leetcode 的 Java 中的两个总和
【发布时间】:2021-08-23 23:02:12
【问题描述】:

我正在处理 leetcode 问题。我刚刚解决了以下问题:

给定一个整数数组,找到两个数字,使它们相加为一个特定的目标数字。

函数 twoSum 应该返回两个数字的索引,使得它们相加到目标,其中 index1 必须小于 index2。请注意,您返回的答案(index1 和 index2)不是从零开始的。

您可以假设每个输入都只有一个解决方案。

输入:数字={2, 7, 11, 15},目标=9 输出: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^2) 并且在 100 个元素的数组中会花费太多时间。尝试将您的算法更改为使用 O(Nlog2N) 算法,甚至更好,将其转换为线性算法。
  • @LuiggiMendoza 谢谢,但解决方案会奏效。问题是效率不高。
  • 再说一次,并不是你当前的解决方案不能解决问题,而是你的算法太慢了

标签: java arrays


【解决方案1】:

那是因为您的解决方案具有 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};
    }

【讨论】:

    【解决方案2】:

    这可能会减少时间问题,但我不能保证它会完全解决问题!

    您可以在找到解决方案 (result) 后立即return 解决方案,正如问题中所述,对于给定问题,只有一个解决方案存在,即

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

    而不是继续搜索输入的其余部分。

    【讨论】:

    • 没有金枪鱼,实际上时间复杂度在这里很重要!不是你打破循环的地方!
    • @Dinal24 我知道;我只是指出一个小的优化。 (我可以假设您现在也在尝试这个问题吗?;))
    • 很好 :) 是否在此处包含解决方案(或者策略)作为答案?
    • programcreek.com/2012/12/leetcode-solution-of-two-sum-in-java 也许没必要,因为这有答案!
    【解决方案3】:

    public int[] twoSum(int[] nums, int target) { int[] 索引 = 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;
    }
    

    }

    【讨论】:

      【解决方案4】:

      这是leetcode question

      当然有一个有效的解决方案。 时间复杂度为 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");
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-25
        • 1970-01-01
        • 2017-11-11
        • 1970-01-01
        • 1970-01-01
        • 2012-12-09
        相关资源
        最近更新 更多