• 算法-1.两数之和


    题目

      给定一个整数数组nums和一个整数目标值target,在该数组中找出和为目标值target两个整数,并返回数组下标。
      假设每种输入只会对应一个答案,但是数组中同一个元素在答案里不能重复出现。
      可以按任意顺序返回答案。

    示例

      示例1
    输入:nums = [2,7,11,15], target = 9
    输出:[0,1]
    解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
    
    • 1
    • 2
    • 3
      示例 2:
    输入:nums = [3,2,4], target = 6
    输出:[1,2]
    
    • 1
    • 2
      示例 3:
    输入:nums = [3,3], target = 6
    输出:[0,1]
    
    • 1
    • 2

    思路

    方法一: 暴力枚举

      枚举数组中的每一个数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];
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    复杂度分析

    • 时间复杂度:O(N的2次方),其中N是数组中的元素数量。最坏情况下数据中任意两个数都要被匹配一次。
    • 空间复杂度:O(1)
    方法二:哈希表

      方法一的时间复杂度较高的原因是寻找target-x的时间复杂度过高。因此,需要一种更优秀的方法:能够快速寻找数组中是否存在目标元素,如果存在,需要找出其索引。
      使用哈希表,可以将寻找target-x的时间复杂度降低到从O(N)降低到O(1).
      创建一个哈希表,对于每一个x,首先查询哈希表中是否存在target-x,然后将x插入到哈希表中,即可保证不会让x与自己匹配。

    class Solution {
        public int[] twoSum(int[] nums, int target) {
            Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
            for (int i = 0; i < nums.length; ++i) {
                if (hashtable.containsKey(target - nums[i])) {
                    return new int[]{hashtable.get(target - nums[i]), i};
                }
                hashtable.put(nums[i], i);
            }
            return new int[0];
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    复杂度分析

    • 时间复杂度:O(N),其中N是数组中的元素数量。对于每一个元素x,可以O(1)地寻找target-x
    • 空间复杂度:O(N),其中N是数组中的元素数量。主要为哈希表的开销

    Java代码

    /**
     * @author huchx
     * @date 2022/6/7 17:15
     */
    public class Test_2 {
        public static int[] twoSum(int[] nums, int target) {
            int[] res = 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){
                        res[0]=i;
                        res[1]=j;
                    }
                }
            }
            return res;
        }
        public static void main(String[] args) {
            int[] nums =new int[3];int target = 6;
            nums[0]=3;
            nums[1]=2;
            nums[2]=4;
            System.out.println(twoSum(nums,target)[0]);
            System.out.println(twoSum(nums,target)[1]);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    Git -- submoudule子模块使用
    抽象工厂模式:构建复杂的对象族
    STL初识
    Java笔记(4)
    企业计算机服务器中了locked勒索病毒怎么办,勒索病毒解密恢复
    Cypress 前端 E2E 测试——手把手入门教程
    Day28_vuex模块化+namespaced_2
    怎么查找计算机SCI文献? - 易智编译EaseEditing
    基于JavaSwing开发MP3音乐播放器系统 课程设计 大作业源码 毕业设计
    图解垃圾回收器运作机制
  • 原文地址:https://blog.csdn.net/qq_32530561/article/details/125515982