网站优化文章,网站制作价格,WordPress设置页面宽度占满,vivo应用商店力扣爆刷第83天之hot100五连刷1-5 文章目录 力扣爆刷第83天之hot100五连刷1-5一、1. 两数之和二、49. 字母异位词分组三、128. 最长连续序列四、283. 移动零五、11. 盛最多水的容器 一、1. 两数之和
题目链接#xff1a;https://leetcode.cn/problems/two-sum/description/?…力扣爆刷第83天之hot100五连刷1-5 文章目录 力扣爆刷第83天之hot100五连刷1-5一、1. 两数之和二、49. 字母异位词分组三、128. 最长连续序列四、283. 移动零五、11. 盛最多水的容器 一、1. 两数之和
题目链接https://leetcode.cn/problems/two-sum/description/?envTypestudy-plan-v2envIdtop-100-liked 思路利用hashmap的特性寻找两数之中的另一个即可。
class Solution {public int[] twoSum(int[] nums, int target) {MapInteger, Integer map new HashMap();for(int i 0; i nums.length; i) {int t target - nums[i];if(map.containsKey(t)) {return new int[] {i, map.get(t)};}else {map.put(nums[i], i);}}return new int[]{};}
}二、49. 字母异位词分组
题目链接https://leetcode.cn/problems/group-anagrams/description/?envTypestudy-plan-v2envIdtop-100-liked 思路要求将字母异位词分组一看就知道需要使用HashMap进行分组收集但是我们需要给每一个分组提供一个key使得同一个分组的字符串经过运算后可以得到一样的key这样就可以基于一样的key利用HashMap收集分组而计算key的方法我选择将每一个字符串排序得到正序字符串用其作为key。
class Solution {public ListListString groupAnagrams(String[] strs) {MapString, ListString map new HashMap();ListListString arrayList new ArrayList();for (String str : strs) {char[] cArray str.toCharArray();Arrays.sort(cArray);String t new String(cArray);ListString value map.getOrDefault(t, new ArrayListString());value.add(str);map.put(t, value);}SetString set map.keySet();for(String s : set) {arrayList.add(map.get(s));}return arrayList;}
}三、128. 最长连续序列
题目链接https://leetcode.cn/problems/longest-consecutive-sequence/description/?envTypestudy-plan-v2envIdtop-100-liked 思路本题是乱序的数组求最长的连续序列如{4, 3, 100, 1, 2}最长连续序列长度是4即为1,2,3,4这种和动态规划中求最长递增子序列最长重复子序列还不同明显是无需的序列还需要把无需给理顺了题目要求O(n)自然不能排序数值范围10的9次方自然不能用数组遍历这种情况下就应该考虑hashset了把所有元素添加set之后遍历只有当前元素是序列的起始元素才会向下求长度只要不是起始元素就不进入while正好所有元素只遍历一遍时间复杂度为O(n)。
class Solution {public int longestConsecutive(int[] nums) {SetInteger set new HashSet();for(int i : nums) {set.add(i);}int max 0;for(int i : set) {if(!set.contains(i-1)) {int cur 1;while(set.contains(i1)) {cur;i;}max Math.max(max, cur);}}return max;}
}四、283. 移动零
题目链接https://leetcode.cn/problems/move-zeroes/description/?envTypestudy-plan-v2envIdtop-100-liked 思路快慢指针快指针正常遍历慢指针不动直到快指针遇到非0元素然后与慢指针交换元素然后慢指针前进一步。原理就是没有0快慢指针就一起动了有0慢指针就被留下了。
class Solution {public void moveZeroes(int[] nums) {int slow 0;for(int i 0; i nums.length; i) {if(nums[i] ! 0) {int temp nums[slow];nums[slow] nums[i];nums[i] temp;slow;}}}
}五、11. 盛最多水的容器
题目链接https://leetcode.cn/problems/container-with-most-water/description/?envTypestudy-plan-v2envIdtop-100-liked 思路area Math.min(nums[left], nums[right]) * (right - left); 当nums[right] nums[left] 时left所能得到的结果是可能大可能下的但right–是一定变小的因为nums[left]决定了上限。 当nums[right]nums[left]时right–是可大可小的但left一定是变的更小的因为此时nums[right]决定了上限。
class Solution {public int maxArea(int[] height) {int max 0, l 0, r height.length-1;while(l r) {int cur Math.min(height[l], height[r]) * (r - l);max Math.max(max, cur);if(height[l] height[r]) {l;}else {r--;}}return max;}
}