每日leetcode-Longest Substring Without Repeating Characters

3,777次阅读
没有评论

共计 843 个字符,预计需要花费 3 分钟才能阅读完成。

原题目:

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;

 }
};

网上也可以搜到其他优秀的源代码,明天博主去找一找,今天还是去洗澡休息了。。。。 每日leetcode-Longest Substring Without Repeating Characters

正文完
请博主喝杯咖啡吧!
post-qrcode
 
admin
版权声明:本站原创文章,由 admin 2016-04-04发表,共计843字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码