前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode 3 Longest Substring Without Repeating Characters

Leetcode 3 Longest Substring Without Repeating Characters

作者头像
triplebee
发布2018-01-12 15:18:43
5180
发布2018-01-12 15:18:43
举报

依旧水题一道,两头往后滑动,用数组映射出现的字符就行。

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

代码语言:javascript
复制
class Solution {
public:
    int lengthOfLongestSubstring(string s) 
    {
        int len=s.length();
        int st=0,en=0,result=0;
        int vis[1005];
        memset(vis,0,sizeof(vis));
        while(en<len)
        {
            int now=s[en];
            while(st<en && vis[now])
            {
                vis[s[st]]=0;
                st++;
            }
            vis[now]=1;
            en++;
            result=max(result,en-st);
        }
        return result;
    }
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-08-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档