Skip to content

Java 两数之和练习

2024/7/26

#Java#bx#算法学习

Java算法练习记录

主要是基于力扣上一些例题

两数之和

https://leetcode.cn/problems/two-sum/description/?envType=study-plan-v2&envId=top-100-liked

暴力枚举

两层循环直接干

class Solution {
    public int[] twoSum(int[] nums, int target) {
        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) {
                    return new int[]{i,j};
                }
            }
        }
        return null;
    }
}

使用哈希表直接匹配

这个写法算法复杂度不高

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<Integer,Integer>();
        for (int i = 0; i < nums.length; i++) {
            int a = target - nums[i];
            if (map.containsKey(a)) {
                return new int[] { map.get(a), i };
        }
            map.put(nums[i], i);
}
        return null;
    }
}

上一篇

Next.js CVE-2025-29927 复现笔记

下一篇

Burp Suite 使用摘记