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 substring is "b", with the length of 1.

译:给定一个字符串,找到没有反复字符的最长子字符串。

例:字符串“abcabcbb"的最大字串位”abc",长度为3。

方法一:以每个字符作为起始字符。计算不反复字符串的长度。并记录最大的串的长度。hash表(一个boolean类型的数组,大小为256,数组的索引可相应字符的ASCII码)记录该字符是否出现过。

public static int lengthOfLongestSubstringA(String s) {
<span style="white-space:pre">	</span>//book存储字符出现的位置。索引表示字符ASCII码,值表示最后一次在字符串中出现的位置
    boolean[] book = new boolean[256];
<span style="white-space:pre">	</span>int maxSubLen = 0;  
<span style="white-space:pre">	</span>int[] lenOff = new int[s.length()];//记录位置 
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>for(int start=0;start<s.length();start++){   
<span style="white-space:pre">		</span>//初始化标志位
<span style="white-space:pre">		</span>for(int i=0;i<256;i++){
<span style="white-space:pre">			</span>book[i] = false;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>for (int p  = start; p  < s.length();p ++) {
<span style="white-space:pre">			</span>int off = s.charAt(p);
<span style="white-space:pre">			</span>
<span style="white-space:pre">			</span>if (book[off]) {//字符出现过 
<span style="white-space:pre">				</span>break;
<span style="white-space:pre">			</span>} 
<span style="white-space:pre">			</span>lenOff[start] = p-start+1; 
<span style="white-space:pre">			</span>book[off] = true;
<span style="white-space:pre">			</span>if (maxSubLen < lenOff[start]){
<span style="white-space:pre">				</span>maxSubLen = lenOff[start];
<span style="white-space:pre">			</span>} 
<span style="white-space:pre">		</span>} 
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>return maxSubLen;
}

方法二: 统计可能出现的最大子字符串。须要在hash表(一个整数数组,数组大小为256,每一个索引相应一个字符的ASCII码)中记录上次出现该字符的位置。仅仅进行一次遍历。假设出现反复字符,依据上次出现该字符的位置。重置可能为最大字符串的起始位置,比方上次出现的位置是n,那下次统计的字符串的起始位置就是n+1。

public static int lengthOfLongestSubstring(String s) {
	//book存储字符出现的位置,索引表示字符ASCII码。值表示最后一次在字符串中出现的位置
    int[] book = new int[128];
	int maxSubLen = 0;
	int subStrStart = 0;
	int[] lenOff = new int[s.length()];

	for(int i =0;i<128;i++)
	    book[i]  = -1;

	for (int p  = 0; p  < s.length();p ++) {
		int off = s.charAt(p); 

		if (book[off]>=subStrStart) {//字符在当前字串中出现过
			subStrStart = book[off]+1; //又一次计算字串
		} 

	    lenOff[subStrStart] = p-subStrStart+1;

		if (maxSubLen < lenOff[subStrStart]){
			maxSubLen = lenOff[subStrStart];
		}
		book[off] = p;//字符最后一次出现的位置
	}
	return maxSubLen;
}
时间: 2024-12-17 13:39:02

LeetCode3 Longest Substring Without Repeating Characters的相关文章

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

[Swift]LeetCode3. 无重复字符的最长子串 | 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.

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

[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 Problem 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 03 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

Java for LeetCode 003 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 最长不重复子串

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