leetcode Algorithms 3. Longest Substring Without Repeating Characters

来源:互联网 发布:pc淘宝首页尺寸大小 编辑:程序博客网 时间:2024/06/05 14:44

题意:最长连续子串,并且字串中没有重复的字符

思路:吊吊的O(n)算法--尺取法;

class Solution {public:    int flag[300];    int lengthOfLongestSubstring(string s) {        int x=s.size();        int st=0,en=0;        memset(flag,0,sizeof(flag));        int ans=0;        while(st<x)        {            while(en<x)            {                if(flag[s[en]])break;                flag[s[en++]]++;            }            flag[s[st]]--;            ans=max(ans,en-st);            //cout<<en<<" "<<st<<" "<<x<<endl;            st++;        }        return ans;    }};

阅读全文
0 0