栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

3. 无重复字符的最长子串-双指针+HashSet-Java

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

3. 无重复字符的最长子串-双指针+HashSet-Java

题目在这里

双指针法+ hashset:
  • hashset 存储无需元素,最适合不过了
  • 用一个指针start指向每次子串开始的位置,向后去计算。每次start向后移动1.
import java.util.HashSet;



// @lc code=start
class Solution {
    public int lengthOfLongestSubstring(String s) {
        if(s == null || s.length() == 0 ){
            return 0;
        }
        int start = 0;
        int i = 0;
        int res = 0;
        HashSet  hs = new HashSet();
        while(i < s.length()){
            if( !hs.contains(s.charAt(i))){
                hs.add(s.charAt(i++));
                res = Math.max(res, i - start);
            }else{
                hs.remove(s.charAt(start++));
            }
        }
        return res;
    }
}
// @lc code=end
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/288621.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号