3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium

Examples:

Description: 

  Given a string, find the length of the longest substring without repeating characters.

Example:

  Given "abcabcbb", the answer is "abc", which the length is 3.

  Given "bbbbb", the answer is "b", with the length of 1.

  Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

 

思路分析:

  1.第一想法:从第一个字符起,逐个计算出最长字串长度,然后到遍历到最后一个字符,得到最大值;

  2.其中逐个计算最长字串部分,跟之前的1.Tow Num 问题几乎一样;

  3.于是可以开始编码了........

   一般测试可以通过,但是,当输入字符串特别长的时候会超时,提交失败...看来的重新想解决的方法了  


C#代码:

 public class Solution {
public int LengthOfLongestSubstring(string s) {
int max = ;
char[] sArr = s.ToCharArray();
for(int i=;i<s.Length;i++){
int tempMax=;
int j =i+;
Hashtable ht = new Hashtable();
ht.Add(i,sArr[i]);
while(j<s.Length&&(!ht.ContainsValue(sArr[j]))){
ht.Add(j,sArr[j]);
j++;
tempMax++;
} if(max<tempMax){
max = tempMax;
}
}
return max;
}
}
上一篇:蜗牛慢慢爬 LeetCode 3. Longest Substring Without Repeating Characters [Difficulty: Medium]


下一篇:初入Python继承