LeetCode3 Longest Substring Without Repeating Characters

Posted llguanli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}






以上是关于LeetCode3 Longest Substring Without Repeating Characters的主要内容,如果未能解决你的问题,请参考以下文章

leetcode3. Longest Substring Without Repeating Characters

Leetcode3---Longest Substring Without Repeating Characters

leetcode3 Longest Substring Without Repeating Characters

LeetCode 3. Longest Substring Without Repeating Characters

3. 没有重复字母的最长子串 [leetcode 3: Longest Substring Without Repeating Characters]

3. 没有重复字母的最长子串 [leetcode 3: Longest Substring Without Repeating Characters]