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

问题:

给定一个字符串,找到最长子串的长度,而不重复字符。

例子:

给定"abcabcbb"的答案是"abc",长度是3。

给定"bbbbb"的答案是"b",长度为1。

给定"pwwkew"的答案是"wke",长度为3.请注意,答案必须是子字符串,"pwke"是子序列,而不是子字符串。

解法一(超时):

 1 class Solution {
 2     public int lengthOfLongestSubstring(String s) {
 3
 4
 5         if(s.length()==0){
 6
 7             return 0;
 8
 9         }
10
11         int max=1;
12         int temp=1;
13
14         for(int i=0;i<s.length();i++){
15
16             temp=1;
17
18             for(int j=i+1;j<s.length();j++){
19
20                 if(!((s.substring(i,j)).contains(s.substring(j,j+1)))){
21
22                     temp++;
23
24                 }else{
25
26                     break;
27
28                 }
29
30             }
31
32             if(max<temp){
33
34                 max=temp;
35
36             }
37
38             if(max>(s.length()-i)){
39
40                 break;
41
42             }
43
44         }
45
46         return max;
47
48     }
49
50 }

解法二(通过):set集合中存放的是从i到j个未重复的字符;若第j个元素重复,则将set集合中重i开始删除直到将重复的元素也删除;若第j个元素没有重复则将j所对应的字符放入set集合中,
并且将从i~j的长度j+1-i与以前记录的最大位重复子串长度max比较,即max=Math.max(j+1-i,max);然后将j++;


 1 class Solution {
 2     public int lengthOfLongestSubstring(String s) {
 3
 4         int i=0;
 5         int j=0;
 6         int n=s.length();
 7         int max=0;//最大子串长度
 8
 9         HashSet<Character> set=new HashSet<Character>();//存放未重复的子串
10
11         while(i<n&&j<n){
12
13             //如果j对应的字符未重复,则将j对应的字符放入set中,并且将从i~j的长度j+1-i与以前记录的最大位重复子串长度max比较,即max=Math.max(j+1-i,max);然后将j++;
14
15             if(!(set.contains(s.charAt(j)))){
16
17                 set.add(s.charAt(j++));
18                 max=Math.max(max,j-i);
19
20             }else{
21
22                 //若从j对应的字符与set中的元素重复,则将重复的元素之前的包括重复的元素删除
23
24                 set.remove(s.charAt(i++));
25
26             }
27
28         }
29
30         return max;
31     }
32
33 }
时间: 2024-10-12 10:09:41

最长不重复字符的子串(Leetcode Longest Substring Without Repeating Characters)的相关文章

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

leetcode——Longest Substring Without Repeating Characters 求链表中无重复字符的最大字串长度(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

[LeetCode] Longest Substring Without Repeating Characters [15]

题目 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 su

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

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

[LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++语言 java语言实现

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: "bbbbb" Output: 1 Explana

LeetCode Longest Substring Without Repeating Characters 最长不重复子串

题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离是多少,若大于max,则更新max.若小于,则不更新.每扫到一个字符就需要更新他的出现位置了.这里边还有个注意点,举例说明: 假如有长为16串 s="arbtbqwecpoiuyca" 当扫到第2个b时,距离上一个b的距离是2:(直接减) 当扫到第2个c时,距离上一个c的距离是6:(直接减

[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

[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

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 of 1.