力扣- 两数字之和(双循环求解以及哈希表求解)

1、原始题目

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

2、源代码

2.1、使用双循环进行求解

时间复杂度:O(N^2)(N表示数组中的元素的个数)
(代码的执行次数之和)

空间复杂度:O(1)
(算法的执行时,需要的临时的运行空间不随着变量 n 的大小进行变化)

package com.example.JavaEE;

/**
 * @author LuoBin
 * @version 1.0
 * @date 2021/7/28 11:36 下午
 */
public class twoSum {
    public static void main(String[] args) {
        int[] arrey = {2,7,11,12};
        int[] testArrey = {1,2,3,4};

        twoAndSum(testArrey,5);
        System.out.println("++=============================++");
        twoSum.twoAndSum(arrey,23);
    }

    public static int[] twoAndSum(int[] nums, int target) {
        int[] inArrey = nums;
        int n = nums.length;
        // 此处的 n 是一开始就会进行确定了,算法的运行时,没有发生变化,所以是空间复杂度是O(1);
        for (int i = 0; i < n; ++i) {
            System.out.println("i的取值是: "+i);
            for (int j = i + 1; j < n; ++j) {
                System.out.println("j的取值是:"+j);
                if (nums[i] + nums[j] == target) {
                    int[] outPut = {i,j};
                    System.out.println(inArrey[i] + "+" + inArrey[j] + "=" +target);

                    for (int a:outPut){
                        System.out.println(a);}
                }
            }
        }

        return new int[0];
    }

}

2.2、使用 HashMap

...后续...

2.3、使用哈希表进行求解题目

package com.数据结构与算法;

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

/**
 * @author LuoBin
 * @version 1.0
 * @date 2021/7/30 10:41 下午
 */
public class HashMapTest {
    public static int[] twoSum(int[] nums,int target){
        int len = nums.length;

        // 初始化哈希表的时候,将其大小进行固定,防止扩容
        Map<Integer,Integer> hashMap = new HashMap<>(len - 1);

        // 第一个元素之前没有相关的元素与之对应所以第一个位置放第一个元素
        hashMap.put(nums[0],0);

        // 从第一个元素开始进行与前面的元素比较,满足目标值减去一个已知值的差
        for (int i = 1;i < len;i++){
            // i 是取到的元素,利用它寻在 另一个和它匹配的数值
            int anotherNumber = target - nums[i];

            // 查询在哈希表中的值是否存在目标值
            if (hashMap.containsKey(anotherNumber)){
                int[] outPut = new int[2];

                outPut[0] = i;

                // 存在的目标值,将其在数组中的位置进行记录
                outPut[1] = hashMap.get(anotherNumber);

                System.out.println("最终的元素的下标为:" + outPut[0] + "   " + outPut[1]);
                System.out.println(nums[i] + " + " + nums[outPut[1]] +  " = " + target);

                // 将得到的数组下标进行返回
                return new int[]{i,hashMap.get(anotherNumber)};
            }
        }
        throw new IllegalArgumentException("找不到这样的两个数,使之两个数的加法结果为目标值");
    }

    public static void main(String[] args) {
        int[] testArray = {50,7,11,210};
        int[] array = {3,7,11,67};

        twoSum(testArray,260);
        System.out.println("++=============================++");

        twoSum(array,70);
        System.out.println("++=============================++");
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值