题目描述:

 找出一个字符串中,不含有相同字符的最长子串。

做法:

 开一个200的bool数组标记,Ascll码 是否已经出现过了。遍历即可。

AC代码:

class Solution {
      int a[200];
public:
    int lengthOfLongestSubstring(string s) {
 
        int l = 0;int r =0;
        int vmax = 0;
        while(s[r]){
            a[s[r]]++;
            while(a[s[r]]==2){
                a[s[l]]--;
                l++;
            }
            vmax = max(vmax,r-l+1);
            r++;
        }
        return vmax;
    }
};
983 / 983 test cases passed.
Status: Accepted
Runtime: 22 ms
posted on 2017-07-12 12:12  朽木の半夏  阅读(87)  评论(0编辑  收藏  举报