日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

0003 无重复字符的最长子串

發(fā)布時間:2024/6/30 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 0003 无重复字符的最长子串 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

給定一個字符串,找出不含有重復字符的最長子串的長度。

示例:

給定?"abcabcbb"?,沒有重復字符的最長子串是?"abc"?,那么長度就是3。

給定?"bbbbb"?,最長的子串就是?"b"?,長度是1。

給定?"pwwkew"?,最長子串是?"wke"?,長度是3。請注意答案必須是一個子串,"pwke"?是?子序列??而不是子串。

public class Solution {public int LengthOfLongestSubstring(string s) {List<int> nexts = new List<int>();for(int i=0; i<s.Length; i++){int j = i + 1;for(; j<s.Length; j++){if(s[j] == s[i]){nexts.Add(j);break;}}if(j == s.Length){nexts.Add(j);}}List<Section> sections = new List<Section>();for(int i=0; i<s.Length; i++){Section section = new Section();section.start = i;section.end = i;bool flag = true;if (nexts[i] == s.Length){if(i == s.Length-1){section.end = s.Length-1;flag = false;}else{int min = nexts[i + 1];int j = i + 2;for (j = i + 2; j < s.Length; j++){if (nexts[j] < min){min = nexts[j];}}section.end = min - 1;flag = false;}}else{int min = nexts[i + 1];int j = i + 2;for (j = i + 2; j < nexts[i]; j++){if (nexts[j] < min){min = nexts[j];}}if(min > nexts[i]){section.end = nexts[i] - 1;}else{section.end = min - 1;}flag = false;}if(flag == true){section.end = nexts[i] - 1;}sections.Add(section);}if(sections.Count <= 0){return 0;}int max = sections[0].end - sections[0].start + 1, pos = 0;for(int i=1; i<sections.Count; i++){int length = sections[i].end - sections[i].start + 1;if(length > max){max = length; pos = i;}}return max; }class Section{public int start;public int end;} }

?

轉載于:https://www.cnblogs.com/lvniao/p/9401613.html

總結

以上是生活随笔為你收集整理的0003 无重复字符的最长子串的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。