LeetCode刷题-003-无重复字符的最长子串

题目地址

https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

总结

这是自己碰见的第一道滑动窗口题目
拿到题,我其实第一想法是暴力枚举,但是想了想时间复杂度太高。
滑动窗口,顾名思义,定义两个指针,来回进行滑动。
这里我画图举个栗子。
在这里插入图片描述
1,2作为移动步骤,先j为左边界,i向右滑动,滑到第二次a的时候,j向右再滑动。

代码

class Solution {
    public int lengthOfLongestSubstring(String s) {
    int[] last = new int[128];
        Arrays.fill(last, -1);
        int start = 0;
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            int index = s.charAt(i);
            start = Math.max(start, last[index] + 1);
            res = Math.max(res, i - start + 1);
            last[index] = i;
        }
        return res;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值