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


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

    Example 1:

    Input: "abcabcbb"
    Output: 3 
    Explanation: The answer is "abc", with the length of 3. 
    

    Example 2:

    Input: "bbbbb"
    Output: 1
    Explanation: The answer is "b", with the length of 1.
    

    Example 3:

    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:

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

    示例 2:

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

    示例 3:

    输入: "pwwkew"
    输出: 3
    解释: 无重复字符的最长子串是 "wke",其长度为 3。
         请注意,答案必须是一个子串,"pwke" 是一个子序列 而不是子串。
    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:

    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);
            }
        }
    }

    =========================Talk is cheap, show me the code=========================

    CSDN博客地址:https://blog.csdn.net/qq_34115899
  • 相关阅读:
    Cocos2d-X 精灵、动作效果
    android动画介绍之 自己定义Animation动画实现qq抖一抖效果
    C#将dataGridView中显示的数据导出到Excel(大数据量超有用版)
    Redis深入之对象
    【POJ 3694】 Network(割边&lt;桥&gt;+LCA)
    Swift学习笔记(十四)——字符,常量字符串与变量字符串
    关于tomcat session机制梳理
    【机器学习算法-python实现】採样算法的简单实现
    分公司=一部门——组合模式
    临渊慕鱼,不如退而结网!
  • 原文地址:https://www.cnblogs.com/lcy0515/p/10807860.html
Copyright © 2020-2023  润新知