leetCode-1: 两数之和

题目描述

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

示例

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

示例 2:
输入:nums = [3,2,4], target = 6
输出:[1,2]

示例 3:
输入:nums = [3,3], target = 6
输出:[0,1]

解题思路

方法一:暴力双循环

步骤
(1)设置两个循环变量 i 和 j,遍历数组;
(2)i 控制外层循环,负责找到最终结果的第一个值;
(3)j 控制内层循环,负责找到最终结果的第二个值;
(4)依次去判断 nums[i] + nums[j] == target,如果是,则将 i 和 j 赋值给最终结果,如果不是,则继续下一次比较
注意:i 和 j 不能重复,所以循环变量 j 开始的位置为 i + 1
时间复杂度

因为需要两个循环变量 i 和 j 去循环比较,因此时间复杂度为 O(n^2)

代码展示
package com.jx.leetcode.easy.twosum;
/**
 * LeetCode第一题:两数之和
 * @author jiaoxian
 */
public class TwoSum {

    public static int[] twoSum1(int[] nums, int target) {
        int[] result = new int[2];
        int length = nums.length;
        for (int i = 0; i < length; i++) {
            for (int j = i + 1; j < length; j++) {
                if (nums[i] + nums[j] == target) {
                    result[0] = i;
                    result[1] = j;
                    break;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {2, 7, 11, 15};
        Long startTime = System.currentTimeMillis();
        int[] result = twoSum1(nums, 9);
        Long endTime = System.currentTimeMillis();
        for (int i : result) {
            System.out.print(i + " ");
        }
        System.out.println();
        System.out.println("耗时: " + (endTime - startTime) + "ms");
    }

}

方法二:使用 HashMap

步骤
(1)开辟额外的 HashMap 空间
(2)使用循环变量 i 去循环数组 nums,将 target - nums[i] 的值作为 key,i 作为 value,put 到 HashMap 中;
(3)循环时,先判断当前 HashMap 中是否已有 nums[i] 的 key,如果有,则拿到 HashMap 中存放的关于当前 nums[i] 的 value 值,和当前循环变量 i,作为最终结果返回,如果没有,则继续 put
说明:对于 nums[] = {2, 7, 11, 15},target = 9 的入参来说:
第一次循环:i = 0,肯定是 put 操作,k-v 键值对为:{7, 0};
第二次循环:i = 1,nums[1] = 7,此时 HashMap 中已有 key 为 7 的数据了,所以直接拿到 HashMap 中存储的 key 为 7 的 value 值 0 和当前循环变量 i = 1 即可
时间复杂度

由于只需要遍历一遍数组,所以时间复杂度为 O(n),但需要额外的 O(n) 的空间复杂度

代码展示
package com.jx.leetcode.easy.twosum;

import java.util.HashMap;

/**
 * LeetCode第一题:两数之和
 * @author jiaoxian
 */
public class TwoSum {

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

    public static void main(String[] args) {
        int[] nums = {2, 7, 11, 15};
        Long startTime = System.currentTimeMillis();
        int[] result = twoSum2(nums, 9);
        Long endTime = System.currentTimeMillis();
        for (int i : result) {
            System.out.print(i + " ");
        }
        System.out.println();
        System.out.println("耗时: " + (endTime - startTime) + "ms");
    }

}

提交链接

https://leetcode-cn.com/problems/two-sum/submissions/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值