Longest Substring Without Repeating Characters(最长无重复字符的子串)

http://www.lintcode.com/zh-cn/problem/longest-substring-without-repeating-characters/?rand=true

import java.util.HashSet;
import java.util.Set;

public class Solution {
    /*
     * @param s: a string
     * @return: an integer
     */
    public int lengthOfLongestSubstring(String s) {
        // write your code here
        Set set = new HashSet<>();
        int res = 0;
        int j = 0;
        int temp = 0;
        //从前向后遍历找到每一个的最长不重复
        for (int i = 0; i < s.length(); i++) {
            while (j < s.length() && !set.contains(s.charAt(j))) {
                set.add(s.charAt(j));
                temp++;
                j++;
            }
            res = Math.max(res, temp);
            temp--;
            set.remove(s.charAt(i));
        }
        return res;
    }
}

你可能感兴趣的:(Longest Substring Without Repeating Characters(最长无重复字符的子串))