[Leet code 3]Longest Substring Without Repeating Characters

1 题目

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.

Hide Tags

Hash Table Two Pointers String

2 思路

开始想的是取得所有不重复的不就行了,后来发现要连续子串。

然后想着每当碰到重复的,就从该点下一位开始重新添加。

后来碰到个bug测试输入,时间超时。

看到别人的思路,使用了题目提示的Two Pointers。并且,table的key,value设置有讲究,key是字母,value是位置。与我开始想的key是位置不一样。

原文链接:

https://leetcode.com/discuss/17939/my-accepted-o-n-java-solution

ok,见代码

3 代码

       public int lengthOfLongestSubstring(String s) {
           Hashtable<Character,Integer> hash=new Hashtable<>();
           int length=s.length();
           int max=0;
           int availablefrom=0;
           for(int i=0;i<length;i++){
               if(hash.containsKey(s.charAt(i))){
                   //int last = the largest index where has the same character (before current index i)
                   int last=(Integer) hash.get(s.charAt(i));
                   //int available-from = the next index from where latest duplication ends (before current index i)
                   //之所以取最大,防止出现abba,遍历到第二个a时,last会比availableform小
                   availablefrom=Math.max(availablefrom, last+1);
               }
             //then the possible substring is located between available-from and i。
               max=Math.max(max, i-availablefrom+1);
               hash.put(s.charAt(i),i);
           }
           return max;
        }
时间: 2024-10-02 14:34:46

[Leet code 3]Longest Substring Without Repeating Characters的相关文章

【Leet Code】Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters Total Accepted: 20506 Total Submissions: 92223My Submissions Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating le

Leet Code 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 subst

[C++]LeetCode: 105 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 s

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, Medium] Longest Substring Without Repeating Characters

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]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: 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 &q

leetcode4 ---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 subst

LeetCode: 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 subst

LeetCode3 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 s