两数之和--算法

鄙人的解法

思路写在注释中

 

 

 

 1 import java.util.HashMap;
 2 import java.util.Map;
 3 
 4 /**
 5  * 算法:两数之和
 6  *
 7  * @author 王子威
 8  * @date 2021/2/3
 9  */
10 public class Wzwsuanfa
11 {
12     public static void main(String[] args)
13     {
14         // 输入:nums = [2,7,11,15], target = 9
15         Integer[] nums = {2,7,11,15};
16         Integer target = 9;
17         int[] getwzw = getwzw(nums, target);
18         System.out.println("getwzw(nums, target) = " + getwzw.toString());
19     }
20 
21     private static int[] getwzw(Integer[] nums, Integer target)
22     {
23         //用来存储遍历过的值【key=数组下标,value=下标对应的值】
24         Map<Integer,Integer> map = new HashMap<>();
25         //循环数组
26         for (int i = 0; i < nums.length; i++)
27         {
28             //
29             Integer hashMapValue = target - nums[i];
30             // 在添加之前需要做计算:target - nums 的值在hashmap的value中是否存在
31             if (map.containsValue(hashMapValue)){
32                 // 进来说明有对应的value值:输出:[0,1]
33                 // 获取所有的key
34                 for (Integer key : map.keySet())
35                 {
36                     // 获取key中的value比较计算出来的value
37                     if (map.get(key).equals(hashMapValue))
38                     {
39                         //返回数组结果
40                         return new int[]{key, i};
41                     }
42                 }
43             }
44             // key=数组下标,value=下标对应的值】
45             map.put(i,nums[i]);
46         }
47         //防止报错
48         return null;
49     }
50 }

作者:王子威
链接:https://www.cnblogs.com/saoge/p/14367549.html

以下是网络上的解法

方法一:

思路
标签:哈希映射
这道题本身如果通过暴力遍历的话也是很容易解决的,时间复杂度在 O(n2)O(n2)
由于哈希查找的时间复杂度为 O(1)O(1),所以可以利用哈希容器 map 降低时间复杂度
遍历数组 nums,i 为当前下标,每个值都判断map中是否存在 target-nums[i] 的 key 值
如果存在则找到了两个值,如果不存在则将当前的 (nums[i],i) 存入 map 中,继续遍历直到找到为止
如果最终都没有结果则抛出异常
时间复杂度:$$

class Solution {
    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};
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }
}

作者:guanpengchn
链接:https://leetcode-cn.com/problems/two-sum/solution/jie-suan-fa-1-liang-shu-zhi-he-by-guanpengchn/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  

方法二:暴力枚举
思路及算法

最容易想到的方法是枚举数组中的每一个数 x,寻找数组中是否存在 target - x。

当我们使用遍历整个数组的方式寻找 target - x 时,需要注意到每一个位于 x 之前的元素都已经和 x 匹配过,因此不需要再进行匹配。而每一个元素不能被使用两次,所以我们只需要在 x 后面的元素中寻找 target - x。

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int n = nums.length;
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                if (nums[i] + nums[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        return new int[0];
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/two-sum/solution/liang-shu-zhi-he-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @ 2021-02-03 16:04  骚哥  阅读(278)  评论(0编辑  收藏  举报