双指针&滑动窗口

  • 单调栈常用于两边 最近 的较大或较小的,双指针常用于两边 最远 的较大或较小的元素。(有时不是最远的,但是可以转换成最远的来进行思考)

leetcode11盛最多水的容器🔐

在这里插入图片描述

在这里插入图片描述

  • 一端低的以后不可能作为容器边界。
class Solution {
    public int maxArea(int[] height) {
        int N = height.length - 1,i = 0, res = 0;
        while (i < N) {
            res = height[i] < height[N] ?
                    Math.max(res, (N - i) * height[i++])
                    : Math.max(res, (N - i) * height[N--]);
        }
        return res;
    }
}

leetcode42接雨水🔐

在这里插入图片描述

class Solution {
    public int trap(int[] height) {
        if (height==null||height.length<=2)return 0;
        int leftMax=height[0],rightMax=height[height.length-1];
        int left=0,right=height.length-2;
        int res=0;
        while (left<=right){
            if (leftMax<rightMax){
                if (height[left]<leftMax){
                    res+=leftMax-height[left];
                }else {
                    leftMax=height[left];
                }
                ++left;
            }else {
                if (height[right]<rightMax){
                    res+=rightMax-height[right];
                }else {
                    rightMax=height[right];
                }
                --right;
            }
        }
        return res;
    }
}

在这里插入图片描述

leetcode3无重复字符的最长子串🔐

在这里插入图片描述
在这里插入图片描述

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int N = s.length();
        if (N <= 1) return N;
        int maxLen = 0;
        int left = -1, right = 0;// 注意left的起始位置
        int[] map = new int[256];
        Arrays.fill(map,-1);// 初始化-1位置
        while (right < N) {
            left = Math.max(left, map[s.charAt(right)]);
            maxLen = Math.max(maxLen, right - left);
            map[s.charAt(right)] = right++;
        }
        return maxLen;
    }
}

leetcode992K个不同整数的子数组🔐

在这里插入图片描述
在这里插入图片描述

  • 恰好K个转化成最多k个,最多k个减去最多k-1个就是恰好k个的结果。
class Solution {
    public int subarraysWithKDistinct(int[] nums, int k) {
        return mostDifferentK(nums, k) - mostDifferentK(nums, k - 1);
    }

    private int mostDifferentK(int[] nums, int k) {
        int left = 0, right = 0, N = nums.length, differentNum = 0, res = 0;
        int[] map = new int[N + 1];
        while (right < N) {
            if (map[nums[right]] == 0) {
                ++differentNum;
            }
            map[nums[right++]]++;
            while (differentNum > k) {
                map[nums[left]]--;
                if (map[nums[left]] == 0) {
                    --differentNum;
                }
                ++left;
            }
            res += right - left;// 定left,该right位置增加所产生的新数组个数
        }
        return res;
    }

}

leetcode141环形链表

在这里插入图片描述
在这里插入图片描述

public class Solution {
    public boolean hasCycle(ListNode head) {
        if (head == null) return false;
        ListNode slow = head;
        ListNode quick = head;
        while (quick.next != null && quick.next.next != null) {
            slow = slow.next;
            quick = quick.next.next;
            if (slow == quick) {
                return true;
            }
        }
        return false;
    }
}

leetcode142环形链表2

在这里插入图片描述
在这里插入图片描述

public class Solution {
    public ListNode detectCycle(ListNode head) {
        // 防止快慢指针压根没有进入第一个循环中,并且无环,这就要先进行判断。
        if(head==null||head.next==null||head.next.next==null)return null;
        ListNode slow = head, quick = head;
        while (quick.next != null && quick.next.next != null) {
            slow = slow.next;
            quick = quick.next.next;
            if (slow == quick) break;
        }
        if (quick != slow) return null;
        quick = head;
        while (slow != quick) {// 当前位置和head走相同步数就是第一个入环节点
            slow = slow.next;
            quick = quick.next;
        }
        return slow;
    }
}

leetcode76最小覆盖子串🔐

在这里插入图片描述
在这里插入图片描述

class Solution {
    public String minWindow(String s, String t) {
        int N = s.length();
        int contains = t.length();
        if (N == 0 || contains == 0) return "";
        int[] map = new int[256];// 数组字典
        for (int i = 0; i < contains; i++) {
            map[t.charAt(i)]++;
        }
        String res = "";
        int minLen = Integer.MAX_VALUE;// 记录结果长度
        for (int left = 0, right = 0; right < N; right++) {
            map[s.charAt(right)]--;
            if (map[s.charAt(right)] >= 0) {// 若为负数表明t中没有
                --contains;//存在一个就还差点字符就少一个
            }
            if (contains == 0) {
                while (map[s.charAt(left)] < 0) {// 小于零说明t中没有该字符或者s中该段序列该字符有多个
                    map[s.charAt(left++)]++;// left尽量向左运行
                }
                // 此时就产生一个结果,就看是否是最短的
                if (minLen > right - left + 1) {
                    res = s.substring(left, right + 1);
                    minLen = right - left + 1;
                }
            }
        }
        return res;
    }
}

leetcode209长度最小的数组

在这里插入图片描述
在这里插入图片描述

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int N = nums.length,left = 0, right = 0, sum = 0, res = Integer.MAX_VALUE;
        while (right < N) {
            sum += nums[right];
            while (sum >= target) {
                res = Math.min(res, right - left + 1);
                sum -= nums[left++];
            }
            right++;
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
}

leetcode567字符串的排列

在这里插入图片描述
在这里插入图片描述

  • 用两个虚拟字典,通过比较虚拟字典进行结果的判断
class Solution {
    public boolean checkInclusion(String s1, String s2) {
        int n = s1.length(), m = s2.length();
        if (n > m) {
            return false;
        }
        int[] cnt1 = new int[26];
        int[] cnt2 = new int[26];
        for (int i = 0; i < n; ++i) {
            ++cnt1[s1.charAt(i) - 'a'];
            ++cnt2[s2.charAt(i) - 'a'];
        }
        if (Arrays.equals(cnt1, cnt2)) {
            return true;
        }
        for (int i = n; i < m; ++i) {
            ++cnt2[s2.charAt(i) - 'a'];
            --cnt2[s2.charAt(i - n) - 'a'];
            if (Arrays.equals(cnt1, cnt2)) {
                return true;
            }
        }
        return false;
    }
}

在这里插入图片描述

  • 一个虚拟字典,本质还是模拟两个字典,造成判断条件较多
class Solution {
    public boolean checkInclusion(String s1, String s2) {
        int M = s1.length();
        int N = s2.length();
        if (N == 0 || N < M) return false;
        int[] map = new int[26];
        Arrays.fill(map, Integer.MIN_VALUE);
        for (int i = 0; i < M; i++) {
            int i1 = s1.charAt(i) - 'a';
            if (map[i1] < -1) {
                map[i1] = 1;
            } else {
                map[i1]++;
            }
        }
        int count = M;// s2中是s1中的有效字符个数
        for (int i = 0; i < M; i++) {
            int j = s2.charAt(i) - 'a';
            if (map[j] != Integer.MIN_VALUE) {
                map[j]--;
                if (map[j] >= 0) {
                    count--;
                }
            }
        }

        if (count == 0) return true;
        for (int left = 0, right = M; right < N; left++, right++) {
            int i = s2.charAt(left) - 'a';
            if (map[i] != Integer.MIN_VALUE) {
                map[i]++;
                if (map[i] > 0) {
                    count++;
                }
            }
            int j = s2.charAt(right) - 'a';
            if (map[j] != Integer.MIN_VALUE) {
                map[j]--;
                if (map[j] >= 0) {
                    count--;
                }
            }
            if (count == 0) return true;
        }
        return false;
    }
}

leetcode239滑动窗口最大值🔐

在这里插入图片描述
在这里插入图片描述

  • 双端队列deque
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        int N = nums.length;
        int[] res = new int[N - k + 1];
        // 实例化双端队列
        Deque<Integer> deque = new ArrayDeque<>();
        // 先将nums前k个数按照规则填入
        for (int i = 0; i < k; i++) {
            while (!deque.isEmpty() && nums[deque.getFirst()] <= nums[i]) {
                deque.pollFirst();
            }
            deque.addFirst(i);// 填入的是索引,以便于唯一确定
        }
        res[0] = nums[deque.getLast()];
        for (int i = k; i < N; i++) {
            // 保持双端队列中索引对应在nums中的数据是降序的。
            while (!deque.isEmpty() && nums[deque.getFirst()] <= nums[i]) {
                deque.pollFirst();
            }
            deque.addFirst(i);// 将此时数据填入
            // 当前双端队列第一个元素(最大值)是否还在滑动窗口中
            if (deque.getLast() == i - k) {
                deque.pollLast();
            }=
            res[i - k + 1] = nums[deque.getLast()];
        }
        return res;
    }
}

leetcode287寻找重复数🔐

在这里插入图片描述
在这里插入图片描述

  • 将整体看成链表,数组下标看成val,数组值看成地址位置next,就相当于头结点val=0,next=nums[ val ] ; 就转换成了链表有无环的问题。
class Solution {
    public int findDuplicate(int[] nums) {
        int slow = 0, quick = 0;
        do {
            slow = nums[slow];
            quick = nums[nums[quick]];
        } while (slow != quick);
        quick = 0;
        do {
            slow = nums[slow];
            quick = nums[quick];
        } while (slow != quick);
        return slow;
    }
}

在这里插入图片描述

class Solution {
    public int findDuplicate(int[] arr) {
  		for (int i = 0; i < arr.length; i++) {
			while (arr[i] != i) {
				// 若果没有这样的return 语句那么可能导致该循环不会退出
				// 该条件的意思是已经存在arr[i]为下标值为arr[i]的值,此时出现俩个arr[i]
				if (arr[arr[i]] == arr[i]) {
					return arr[i];
				}
				// 交换此时下标为i和下标为arr[i]的值
				int temp = arr[i];
				arr[i] = arr[temp];
				arr[temp] = temp;
			}
		}
		return 0;
    }
}

leetcode763划分字母区间

在这里插入图片描述
在这里插入图片描述

  • 主要是贪心,使用map寻找索引,使用双指针定长度。
 class Solution {
    public List<Integer> partitionLabels(String s) {
        int[] map = new int[26];
        for (int i = 0; i < s.length(); i++) {
            map[s.charAt(i) - 'a'] = i;
        }
        int lastIndex = -1, nextIndex = 0;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            nextIndex = Math.max(nextIndex, map[s.charAt(i) - 'a']);
            if (i == nextIndex) {
                list.add(i - lastIndex);
                lastIndex = i;
            }
        }
        return list;
    }
}

leetcode19删除链表的倒数第N个节点

在这里插入图片描述

在这里插入图片描述

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode tail = head;
        for (int i = 0; i < n; i++) tail = tail.next;
        if (tail == null) return head.next;// 若删除的是第一个,此时tail为null,直接返回结果
        ListNode pre = head;
        while (tail.next != null) {
            tail = tail.next;
            pre = pre.next;
        }
        // 此时的pre就是删除节点的前驱节点
        pre.next = pre.next.next;
        return head;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「 25' h 」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值