Skip to content

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;
    }
}
编辑这篇文章

评论区

使用 GitHub Discussions 驱动,欢迎留言交流。

上一篇
Burp杂项
下一篇
TCP/IP-Core Protocols