一.算法題
- 題目
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
- Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
二.算法題解讀
-
題目大意:給定一個字符串,找出不含有重複字符的最長子串的長度
- 解讀Example
- 給定"abcabcbb",沒有重複字符的最長子串是"abc",那麼長度就是3
- 給定"bbbbb",最長子串就是"b",長度就是1
- 給定pwwkew,最長子串就是"wke",長度爲3,
- ==注意,==必須是一個子串."pwke",是子序列,而不是子串
三.暴力解決方案
3.1 思路
逐個檢查所有的子字符串,看它是否不含有重複字符
3.2 算法
爲了枚舉給定字符串的所有子字符串,我們需要枚舉它們開始和結束的索引,假如開始和結束的索引分別是i和j.那麼我們有0<=i<=j<=n.因此,使用i從0到n-1以及j從i+1到n這2個嵌套循環.我們就可以遍歷出a的所有子字符串.
3.3 複雜的分析
- 時間複雜度:
o(n3);
- 空間複雜度:
o(min(n,m));
3.4 參考代碼
//(2)無重複字符的最長子串
//求字符串長度函數
int strLength(char *p)
{
int number = 0;
while (*p) {
number++;
p++;
}
return number;
}
//判斷子字符在字符串中是否唯一
int unRepeatStr(char *a,int start,int end)
{
for (int i=start;i<end xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed> j-i)?ans:j-i;
}
}
}
return ans;
}
int main(int argc, const char * argv[]) {
//2)無重複子串的最長子串
char *s = "pwwkew";
int n = LengthLongestSubstring(s);
printf("%d",n);
return 0;
}
小編給大家推薦一個iOS技術交流羣:551346706!
羣內提供數據結構與算法、底層進階、swift、逆向、底層面試題整合文檔等免費資料!