链接:https://leetcode-cn.com/problems/first-missing-positive 分析: 要求时间复杂度为O(N),并且只能使用常数级别的空间 根据抽屉原理:数组的长度n,则答案最大只能是n+1; 可以使用hash函数将答案空间映射到长度为n+1的数组上,再遍历数组找到最小的没出现的正整数。 为实现常数空间复杂度,可以使用原数组的空间...
Design a HashMap without using any built-in hash table libraries. Implement theMyHashMapclass: MyHashMap()initializes the object with an empty map. void put(int key, int value)inserts a(key, value)pair into the HashMap. If thekeyalready exists in the map, update the correspondingvalue. ...
https://leetcode.com/problems/sort-list/https://leetcode.com/problems/remove-linked-list-elements...
publicint[]twoSum(int[]nums,int target){//map的key是nums[i]的值,value是下标iMap<Integer,Integer>map=newHashMap<>();for(int i=0;i<nums.length;i++){//获取结果值与nums[i]的差值int diff=target-nums[i];//如果包含的话,返回结果if(map.containsKey(diff)){returnnewint[]{map.get(diff)...
用HashMap存储某个count第一次出现的位置,后续判断是否存在这个key,就可以计算maxLen了 LeetCode560 Medium 和为K的子数组 leetcode-cn.com/problem LeetCode848 Medium 字母移位 leetcode-cn.com/problem 从后往前处理。 12.子数组类问题 特点:i是起始位置 j是终点位置 LeetCode718 最长重复子数组 leetcode-cn...
Answers for the leetcode problems Solutions MAY vary with offical LeetCode Solutions. PLEASE do a pull request for more elegant solutions(New Issue Request Template, make sure to read it!). Tips for beginners: If you are spending too much time on a problem, look and interpret the solutions...
242 242. Valid Anagram.java Easy [Hash Table, Sort] O(n) O(1), unique chars Java 505 340 340. Longest Substring with At Most K Distinct Characters.java Hard [Hash Table, LinkedHashMap, Sliding Window, String, Two Pointers] O(n) O(k) Java 506 217 217. Contains Duplicate.java Easy...
一遍hashmap 解法 代码语言:javascript 代码运行次数:0 运行 AI代码解释 class Solution { public int[] twoSum(int[] nums, int target) { /* 使用hashmap */ int[] res = new int[2]; Map<Integer,Integer> map = new HashMap<Integer,Integer>(); for(int i = 0 ; i<nums.length;i++){ ...
一. 树 1)求二叉树的高度(Maximum Depth of Binary Tree)// LeetCode, Maximum Depth of Binary ...
算法: public int maxPoints(Point[] points) { if (points.length == 0) { return 0; } else if (points.length == 1) { return 1; } int max = 1; for (int i = 0; i < points.length; i++) { HashMap<Double, Integer> counts = new HashMap<Double, Integer>();// 斜率和该斜率...