前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode3. 无重复字符的最长子串

LeetCode3. 无重复字符的最长子串

作者头像
mathor
发布2018-09-19 15:25:51
8550
发布2018-09-19 15:25:51
举报
文章被收录于专栏:mathormathor
题目链接:LeetCode3

 首先定义一个Map<Character,Integer>用来保存当前字符最后一次出现的下标位置。再定义一个pre数组,用来保存以每一个字符为结尾的最长无重复字符的子串。  接下来可以这么想,假设遍历到了下标i,i位置对应的字符为c,第一种情况,如果c从来没有在Map中出现过,并且i不为0(也就是说c不为首字符),那么per[i] = pre[i - 1] + 1,并且把<c,i>放入Map中。  第二种情况,c出现在Map中,并且通过Map.get(c)得到c最后一次出现的位置记为lastPos,记aPos = lastPos + 1,unRepeatLen = per[i - 1],bPos = i - unRepeatLen,如果aPos >= bPos,则pre[i] = i - aPos + 1,否则pre[i] = i - bPos + 1,并且无论何种情况都要记得更新字符最后一次出现的位置

代码语言:javascript
复制
class Solution {
    public int lengthOfLongestSubstring(String s) {
        if(s.length() == 0)
            return 0;
        Map<Character,Integer> pos = new HashMap<Character,Integer>();
        int[] pre = new int[s.length()];
        char[] str = s.toCharArray();
        for(int i = 0;i < s.length();i++) {
            Integer lastPos = pos.get(str[i]);
            if(lastPos == null) {
                pre[i] = i == 0 ? 1 : pre[i - 1] + 1;
                pos.put(str[i],i);
            } else {
                int aPos = lastPos + 1;
                int repeatLen = pre[i - 1];
                int bPos = i - repeatLen;
                if(aPos >= bPos)
                    pre[i] = i - aPos + 1;
                else
                    pre[i] = i - bPos + 1;
                pos.put(str[i],i);
            }
        }
        int max = pre[0];
        for(int i : pre)
            max = Math.max(max,i);
        return max;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-09-18,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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