3 无重复字符的最长字串

  1. class Solution {
  2. public int lengthOfLongestSubstring(String s) {
  3. if (s == null)
  4. return 0;
  5. int left = 0;
  6. int right = -1;
  7. int maxLength = 0;
  8. Set<Character> set = new HashSet<Character>();
  9. for (left = 0; left < s.length(); left++) {
  10. if (left != 0)
  11. set.remove(s.charAt(left- 1));
  12. while (right + 1 < s.length() && !set.contains(s.charAt(right + 1))) {
  13. set.add(s.charAt(right + 1));
  14. ++right;
  15. }
  16. maxLength = Math.max(maxLength, right - left + 1);
  17. }
  18. return maxLength;
  19. }
  20. }