• 为了保证你在浏览本网站时有着更好的体验，建议使用类似Chrome、Firefox之类的浏览器~~
• 如果你喜欢本站的内容何不Ctrl+D收藏一下呢，与大家一起分享各种编程知识~
• 本网站研究机器学习、计算机视觉、模式识别~当然不局限于此，生命在于折腾，何不年轻时多折腾一下

每日leetcode-Longest Substring Without Repeating Characters

3年前 (2016-04-04) 1484次浏览

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.

class Solution {
public:
int lengthOfLongestSubstring(string s) {
if (s == "")
{
return 0;
}

int startnum = 0;
int sublength = 1;
int oldlength = 1;
bool upflag = true;
while (startnum + sublength<s.length())
{
for (int i = startnum; i<startnum + sublength; i++)
{
if (startnum + sublength<s.length() && s[startnum + sublength] == s[i])
{
upflag = false;
sublength = startnum + sublength - i;
startnum = i + 1;
if (sublength>oldlength)
{
oldlength = sublength;
}
break;
}
}
if (upflag)
{
sublength++;
if (sublength > oldlength)
{
oldlength = sublength;
}
}
upflag = true;
}
return oldlength;

}
};

Deeplearn, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明每日 leetcode-Longest Substring Without Repeating Characters

• 版权声明

本站的文章和资源来自互联网或者站长
的原创，按照 CC BY -NC -SA 3.0 CN
协议发布和共享，转载或引用本站文章
应遵循相同协议。如果有侵犯版权的资
源请尽快联系站长，我们会在24h内删
除有争议的资源。
• 网站驱动

• 友情链接

• 支持主题

邮箱：service@deeplearn.me