其他分享
首页 > 其他分享> > LeetCode No.3 无重复字符的最长子串

LeetCode No.3 无重复字符的最长子串

作者:互联网

import java.util.HashSet;

class Solution {
    public int lengthOfLongestSubstring(String s) {
        // 滑动窗口
        int maxLength = 0; 
        for (int i = 0; i < s.length(); i ++) {
            int length = 0; 
            HashSet<Character> set = new HashSet<>();
            int j = i;
            while (j < s.length() && set.add(s.charAt(j)) == true) {
                length ++;
                j ++; 
            }

            if (length > maxLength) 
                maxLength = length;
        }

        return maxLength; 
    }
}

 

标签:子串,set,HashSet,int,++,length,No.3,maxLength,LeetCode
来源: https://www.cnblogs.com/leetcode-ye/p/16500502.html