随笔分类 - LeetCode
摘要:滑动窗口 import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Solution { public List<Integer> findAnagrams(String s, String p
阅读全文
摘要:滑动窗口 class Solution { public int lengthOfLongestSubstring(String s) { /** * 滑动窗口 * 哈希表存储字符的个数,如果大于0说明重复了 */ int[] count = new int[256]; int left = 0;
阅读全文
摘要:滑动窗口法 class Solution { public int minSubArrayLen(int target, int[] nums) { /** * 滑动窗口 * 两个指针同向移动,如果当前窗口内的元素和大于target就记录长度,然后left指针右移缩小查找范围 * 如果小于targe
阅读全文
摘要:暴力解法(超时) import java.util.Arrays; class Solution { public int maxArea(int[] height) { /** * 暴力解法,直接使用额外数组存放每一个坐标为左边界时的最大面积,最后对数组求最大值 */ int[] res = ne
阅读全文
摘要:双指针法 class Solution { public String reverseVowels(String s) { /** * 双指针法遍历字符串 * s.toCharArray()方法将字符串转为字符数组 * new String(chars)将字符数组转换为字符串(直接用toString
阅读全文
摘要:双指针法 class Solution { public void reverseString(char[] s) { /** * 双指针遍历 */ int left = 0; int right = s.length - 1; char c; while (left < right){ c = s
阅读全文
摘要:双指针法 class Solution { public boolean isPalindrome(String s) { /** * 双指针遍历 * 定义一个判断字符是否为字母的方法 */ int left = 0; int right = s.length() - 1; s = s.toLowe
阅读全文
摘要:二分查找法 class Solution { public int[] twoSum(int[] numbers, int target) { int[] res = new int[2]; /** * 对于每一个元素,在后面的子数组中寻找是否有匹配的元素 * 因为数组是有序的,使用二分查找法寻找更
阅读全文
摘要:双指针法(归并排序法) import java.util.Arrays; class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { /** * 双指针遍历 * 先将两个子数组简单合并,然后分别从子数组第一
阅读全文
摘要:双指针法 class Solution { public int removeDuplicates(int[] nums) { /** * 双指针遍历数组 * 定义一个flag来指示重复元素出现的次数 */ int n = 0; int flag = 0; for (int i = 1; i < n
阅读全文
摘要:双指针法 class Solution { public int removeElement(int[] nums, int val) { /** * 使用双指针从两端遍历,start指向当前遍历元素,end指向最后一个待换元素 * 当遇到目标元素时,和最后一个元素互换 * 头指针等于尾指针时结束循
阅读全文
摘要:双指针法 class Solution { public int removeDuplicates(int[] nums) { /** * 同《80. 删除有序数组中的重复项II》 */ if (nums.length == 1){ return 1; } int length = 1; for (
阅读全文
摘要:暴力解法 class Solution { public void moveZeroes(int[] nums) { /** * 使用额外的数组存放非零元素 */ int[] temp = new int[nums.length]; int index = 0; for (int i = 0; i
阅读全文
摘要:哈希表 class Solution { public int firstUniqChar(String s) { int[] arr = new int[26]; /** * 先将所有字母的词频保存下来 */ for (int i = 0; i < s.length(); i++) { arr[s
阅读全文
摘要:import java.util.TreeMap; class MapSum { /** * 本题中每个节点还有一个对应的val,可以替代isWord的作用 */ class Node{ int val; TreeMap<Character, Node> next; public Node(int
阅读全文
摘要:import java.util.TreeMap; class WordDictionary { class Node{ boolean isWord; TreeMap<Character, Node> next; public Node(){ isWord = false; next = new
阅读全文
摘要:import java.util.TreeMap; class Trie { class Node{ boolean isWord; TreeMap<Character, Node> next; public Node(){ next = new TreeMap<>(); isWord = fals
阅读全文
摘要:import java.util.TreeMap; class MyCalendar { TreeMap<Integer, Integer> treeMap; public MyCalendar() { treeMap = new TreeMap(); } public boolean book(i
阅读全文
摘要:class NumArray { SegmentTree segmentTree; public NumArray(int[] nums) { segmentTree = new SegmentTree(nums); } public void update(int index, int val)
阅读全文
摘要:/** * 对于不可变数组,用线段树反而更麻烦 */ class NumArray { int[] sum; public NumArray(int[] nums) { /** * 提前保存前n个元素的和,sum[i]保存的是前i个元素,不包括自身 * 区间和就是左右边界前缀和的差 */ sum =
阅读全文

浙公网安备 33010602011771号