• 算法-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
  • 相关阅读:
    浏览器控制台中网络选项看不到请求发送出的url信息解决办法
    git的基本操作
    Jmeter接口自动化(七)后置处理器
    形式化验证笔记
    嵌入式开发:用DSP软件替代模拟组件的5大好处
    Android IPC | Android多进程模式
    (附源码)springboot农田灌溉设施管理系统 毕业设计 260931
    XiaodiSec day028 Learn Note 小迪安全学习笔记
    【安全】 Java 过滤器 解决存储型xss攻击问题
    Improved Baselines with Momentum Contrastive Learning 论文学习
  • 原文地址:https://blog.csdn.net/qq_32530561/article/details/125515982