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

3. Longest Substring Without Repeating Characters(无重复字符的最长子串)

作者头像
砖业洋__
发布2023-05-06 19:10:59
970
发布2023-05-06 19:10:59
举报
文章被收录于专栏:博客迁移同步博客迁移同步

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

Example 1:

代码语言:javascript
复制
Input: "abcabcbb"
Output: 3 
Explanation: The answer is "abc", with the length of 3. 

Example 2:

代码语言:javascript
复制
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

代码语言:javascript
复制
Input: "pwwkew"
Output: 3
Explanation: 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.

给定一个字符串,找出不含有重复字符的最长子串的长度。

示例 1:

代码语言:javascript
复制
输入: "abcabcbb"
输出: 3 
解释: 无重复字符的最长子串是 "abc",其长度为 3。

示例 2:

代码语言:javascript
复制
输入: "bbbbb"
输出: 1
解释: 无重复字符的最长子串是 "b",其长度为 1。

示例 3:

代码语言:javascript
复制
输入: "pwwkew"
输出: 3
解释: 无重复字符的最长子串是 "wke",其长度为 3。
     请注意,答案必须是一个子串,"pwke" 是一个子序列 而不是子串。
代码语言:javascript
复制
class Solution {
        public int lengthOfLongestSubstring(String s) {
        char[] c = s.toCharArray();
        List<Character> list = new LinkedList<Character>();
        int max = 0;
        for (int i = 0; i < c.length; ++i) {
            if (list.contains(c[i])) {
                max = Math.max(max, list.size());
                list.remove(0);
                while (list.contains(c[i])) {
                    list.remove(0);
                }
                list.add(c[i]);
            } else {
                list.add(c[i]);
            }
        }
        max = Math.max(max, list.size());
        return max;
    }
}

迅雷笔试题2,采用leetcode原题。

Debug code in playground:

代码语言:javascript
复制
class Solution {
        public int lengthOfLongestSubstring(String s) {
        char[] c = s.toCharArray();
        List<Character> list = new LinkedList<Character>();
        int max = 0;
        for (int i = 0; i < c.length; ++i) {
            if (list.contains(c[i])) {
                max = Math.max(max, list.size());
                list.remove(0);
                while (list.contains(c[i])) {
                    list.remove(0);
                }
                list.add(c[i]);
            } else {
                list.add(c[i]);
            }
        }
        max = Math.max(max, list.size());
        return max;
    }
}

public class MainClass {
    public static String stringToString(String input) {
        return JsonArray.readFrom("[" + input + "]").get(0).asString();
    }
    
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            String s = stringToString(line);
            
            int ret = new Solution().lengthOfLongestSubstring(s);
            
            String out = String.valueOf(ret);
            
            System.out.print(out);
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-09-13,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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