Given a string, find the length of the longest substring without repeating characters.面試
- Given "abcabcbb", the answer is "abc", which the length is 3.
題目大意:給定一個字符串,找出不含有重複字符的最長子串的長度算法
解讀Exampleswift
- 給定"abcabcbb",沒有重複字符的最長子串是"abc",那麼長度就是3
3.1 思路bash
逐個檢查全部的子字符串,看它是否不含有重複字符數據結構
3.2 算法xss
爲了枚舉給定字符串的全部子字符串,咱們須要枚舉它們開始和結束的索引,假如開始和結束的索引分別是i和j.那麼咱們有0<=i<=j<=n.所以,使用i從0到n-1以及j從i+1到n這2個嵌套循環.咱們就能夠遍歷出a的全部子字符串.函數
3.3 複雜的分析ui
o(n3);
o(min(n,m));
3.4 參考代碼spa
//(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、逆向、底層面試題整合文檔等免費資料! code