Java数据结构与算法面试题 两数之合 作者:哇塞大嘴好帅

Java数据结构与算法面试题 两数之合 作者:哇塞大嘴好帅

​ 作者:哇塞大嘴好帅

​ 哇塞大嘴好帥

1.题目 该题目来自LeetCode

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

假如我们输入

Intput : nums = [3,2,7],target = 10
Output: [0,2]

通过这个可知 数组下标为0和2的数据相加得target

2.实现

package com.dazuizui.twosum;

import java.util.HashMap;
import java.util.Map;

/**
 *  Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
 *
 *  You may assume that each input would have exactly one solution, and you may not use the same element twice.
 *
 *  You can return the answer in any order.
 *
 *  来源:力扣(LeetCode)
 *  链接:https://leetcode-cn.com/problems/two-sum
 *  著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 *
 *  @author 杨易达
 *  @Time   2020-11-22 20:24
 */
public class Demo1 {
    public static void main(String[] args) {
        int[] nums = new int[]{2,7,11,15};
        Solution solution = new Solution();
        solution.twoSum2(nums,13);
    }
}

class Solution {
    //方法A
    public int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    System.out.println(i+"  "+j);
                    return new int[]{i, j};
                }
            }
        }
        return new int[]{0};
    }

    //方法B
    public int[] twoSum2(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])){
                System.out.println(map.get(target-nums[i])+"  "+i);
                return new int[]{map.get(target-nums[i]),i};
            }

            map.put(nums[i],i);
        }
        return new int[]{0};
    }
}

2.1方法A解析 枚举方法

    //方法A
    public int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    System.out.println(i+"  "+j);
                    return new int[]{i, j};
                }
            }
        }
        return new int[]{0};
    }

此方法A也叫做枚举方法

​ 我们2个嵌套for循环,当走到if判断的时候,假设这时候i=0 j = i+1 这时候我们就判断 int[0] + int [1] == target是否满足,如果不满足则进入下一次循环,int[0] + int [2] == targetint[1] + int [2] == target

int[0] + int [3] == target

​ 当某一次循环满足此条件,则返回一个新的数组,他的值为{i, j},这两个值就是数组的某2个下标的值相加等于target。

该方法性能分析

​ 该分析由LeetCode提供

52 / 52 个通过测试用例
状态:通过
执行用时: 0 ms
内存消耗: 38.3 MB

2.2方法B解析 哈希表

//方法B
public int[] twoSum2(int[] nums, int target) {
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < nums.length; i++) {
        if (map.containsKey(target - nums[i])){
            System.out.println(map.get(target-nums[i])+"  "+i);
            return new int[]{map.get(target-nums[i]),i};
        }
        map.put(nums[i],i);
    }
    return new int[]{0};
}

什么是Hash表?

​ 散列表(Hash table,也叫哈希表),是根据(Key value)访问的数据结构。

代码解析

​ 首先创建一个HashMap,他们的键值类型都是Integer,

​ 接下来一个for循环,里面有个if判断,如果target - nums[i]的返回值在Hashmap存在此Key就代表找到了两数之和的两数。

target - nums[i] 解析

​ 已知我们输入的数据为

Intput : nums = [3,2,7],target = 10

​ 假设现在i为0, 转换为数字就是 10 - 3 = 7;这时候我们HashMap没有7这个键。我们就HashMap.put()的一个数据, 键值为nums[i] 值为 i

​ 当我们for循环到第N个 map.containsKey(target - nums[i]) 发现该表达式为True就代表找到了我们想要的值。 如果它目前合法把他转换为数字,10 - 7 = 3; 我们发现我们Hash表中有这个键,意味着我们就找到了我们想要的东西。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哇塞大嘴好帅(DaZuiZui)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值