LeetCode【3】.Longest Substring Without Repeating Characters--算法图解及java实现

第三道题Longest
Substring Without Repeating Characters
如下:

    Given
a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

图一、本题窗口扫描示意图

思路:如上图(画得不好请见谅)。维护一个窗口[walker,runner]:

、窗口区间上限runner跑在前面,每扫描一次,将对应的元素添加至HashSet里,直到扫描到HashSet里已经存在的元素,runner停下,记为runner1。等一等走得比较慢的walker。此时,窗口内存在与walker处相同的元素;

、首先记录walker与runner的距离,因为此时walker往前的子字符串都是无元素重复的,与max比大小。紧接着,walker也不能落后,走了起来,一直到与runner一样元素的地方。边走边把之前那些元素从HashSet里丢掉,直到找到那个与runner1元素相同的地方,记为walker1。walker1前面部分丢掉的原因是不可能找到一个包含[walker1,runner1]区间的再大的区间使得其实无重复的,因为[walker1,runner1]区间内已有重复元素。所以求下一个目标区间只能往前走,把下区间丢掉,此时去到第三步;

、把walker1前面(包括自身)丢掉后,问题回到了第一步,重复第一步的过程即可。

Java代码如下;

public class Solution {  
  public int lengthOfLongestSubstring(String s) {  
    if(s==null && s.length()==0)  
        return 0;  
        
    HashSet<Character> set = new HashSet<Character>();  
    int max = 0;  
    int walker = 0;  
    int runner = 0;  
    for(;runner<s.length();runner++)  
    {  
        if(set.contains(s.charAt(runner)))  
        {  
            max = (runner-walker)>max?(runner-walker):max;
            
            while(s.charAt(walker)!=s.charAt(runner))  
            {  
                set.remove(s.charAt(walker));  
                walker++;  
            }  
            walker++;  
        }  
        else  
        {  
            set.add(s.charAt(runner));  
        }  
    }  
    max = (runner-walker)>max?(runner-walker):max;  
    return max;  
   }  
}  

学习参考:

[1]. LeetCode
– Longest Substring Without Repeating Characters (Java)
[2]. Repeating
Characters -- LeetCode

时间: 2024-10-16 19:00:24

LeetCode【3】.Longest Substring Without Repeating Characters--算法图解及java实现的相关文章

【leetcode】 Longest Substring Without Repeating Characters

题目: 给定一个字符串,返回该串没有重复字符的最长子串. 分析: 1)子串:子串要求是连续的. 2)无重复,出现重复就断了,必须从新的位置开始.而新的位置就是重复字符第一次出现位置的下一个位置. 3)整个串可能没有一处重复. 那么,为了找出当前访问的字符是否出现过,要怎么做呢?当然是hash,O(1)的时间,而且既然是字符, 定义个255的hash table 就可以了,hash table 中的元素为相应字符在s中出现的位置.初始化为-1,表示都没有出现. 我们另外定义一个start 和end

leetcode -day21 Longest Substring Without Repeating Characters

 1.Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the

[LeetCode] 5. Longest Substring Without Repeating Characters 最长回文子串

[LeetCode] 5. Longest Substring Without Repeating Characters Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: "babad" Output: "bab" Note: "aba&qu

LeetCode题解 || Longest Substring Without Repeating Characters (O(n)算法)问题

problem: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the long

[LeetCode] 3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

[LeetCode][Python]Longest Substring Without Repeating Characters

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, find the length of the longest substring without repeating characters.For example, the longest s

LeetCode之 Longest Substring Without Repeating Characters

问题描述 Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length o

leetcode 3. Longest Substring Without Repeating Characters (Python版)

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s

[LeetCode][JavaScript]Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the lengt

Leetcode 3 Longest Substring Without Repeating Characters. (最长无重复字符子串) (滑动窗口, 双指针)

目录 问题描述 例子 方法 Leetcode 3 问题描述 Given a string, find the length of the longest substring without repeating characters. 例子 Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: &q