LeetCode 3. Longest Substring Without Repeating Characters

https://leetcode.com/submissions/detail/84675571/

尺取法路过,尾指针在可以添加的时候向后,当有重复就把头指针向后挪。因为只有字符,判断重复直接拿数组搞掉。

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int st=0,en=0,ans=0,cnt=0;
        int vis[128];
        fill(vis,vis+128,0);
        while(en!=s.length())
        {
            if(!vis[s[en]])
            {
                cnt++;
                vis[s[en++]]=1;
                if(ans<cnt) ans=cnt;
            }
            else
            {
                while(vis[s[en]])
                {
                    vis[s[st++]]=0;
                    cnt--;
                }
            }
        }
        return ans;
    }
};

 

posted @ 2016-12-04 21:39  Luke_Ye  阅读(78)  评论(0编辑  收藏  举报