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

Leetcode3——Longest Substring Without Repeating Characters

作者头像
Tyan
发布2019-05-25 23:11:46
3070
发布2019-05-25 23:11:46
举报
文章被收录于专栏:SnailTyan

1. 问题描述

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

Examples:

代码语言:javascript
复制
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.

2. 求解

方法一

当遍历第i个字符时,需要判断[index,i-1]的字符中是否有与s[i]重复的字符,如果字符s[j]与s[i]重复,index直接变为j + 1,重新计算不重复字符的数量,如果[index,i-1]的字符中没有与s[i]重复的字符,则不重复字符计数count++

代码语言:javascript
复制
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int max = 0;
        int count = 0;
        int index = 0;
        //[index,i-1]中是否有与s[i]重复的字符
        boolean flag = false;
        for(int i = 0; i < s.length(); i++) {
            flag = false;
            char ch = s.charAt(i);
            //如果s[j]与s[i]重复,index直接变为j + 1,重新计算不重复字符数
            for(int j = index; j < i; j++) {
                if(s.charAt(j) == s.charAt(i)) {
                    flag = true;
                    index = j + 1;
                    count = i - j;
                    break;
                }
            }
            if(!flag) {
                count++;
                if(count > max) {
                    max = count;
                }
            }
        }
        return max;
    }
}

方法二 方法二的思路是看到有重复问题,首先想到哈希表,由于求解的是不重复子串,因此需要将子串分为两部分,一部分为(i,n-1),一部分为(j,i),如果s[i]不在(j,i)中,则将s[i]放入哈希表中,同时计数器加1,如果s[i]在(j,i)中,则找到(j,i)中与s[i]重复的字符ch,将其移除,当然ch之前的字符也要将其从哈希表中移除,因为包含ch的子串一定与s[i]重复,每移除一个字符,j++。重复上述过程,直至i到字符串最后。每一个找的子串是从(j,i)不重复的最长子串。这里的j是方法一中的index。思路与方法一是一致的,区别是使用哈希表来判断重复而不是使用循环。

代码语言:javascript
复制
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        Map<Character, Character> map = new HashMap<Character, Character>();
        int max = 0;
        int count = 0;
        int j = 0;
        for(int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            //如果有重复字符,逐个移除字符,直到移除了与第i个字符重复的字符
            if(map.containsKey(ch)) {
                while(map.containsKey(ch)) {
                    map.remove(s.charAt(j));
                    j++;
                    count--;
                }
                count++;
                map.put(ch, ch);
            }else {
                map.put(ch, ch);
                count++;
                if(count > max) {
                    max = count;
                }
            }
        }
        return max;
    }
}

备注:Leetcode测试时,发现方法一比方法二速度更快。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年04月01日,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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