`

Leetcode - Longest Substring Without Repeating Characters

 
阅读更多
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
[分析] 下面给出的两种方法基本思路一致,使用两指针表示滑动窗口的左右边界,窗口中的
字符均不重复。若遇到新字符,右指针往前走,若遇到窗口中已有字符,寻找窗口中该字符的下标idx,左指针移到idx下一个位置。方法1是O(n^2),方法2是O(n)的, 方法2的高效之处在于使用一个table保存了所遇字符上一次的出现位置,因此可以在单位时间内完成更新左指针操作。

    // Method 2 : 开一个字符集大小的数组存储遍历s时遇到的字符的最近一次位置,
    //相较Method 1, 节省了寻找上次出现位置以及从HashSet删除新窗口外的字符操作开销
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0)
            return 0;
        int n = s.length();
        int max = 0;
        int[] lastPosTable = new int[256];
        for (int i = 0; i < 256; i++)
            lastPosTable[i] = -1;
        int i = 0, j = 0;
        while (j < n) {
            int lastPos = lastPosTable[s.charAt(j)];
            if (lastPos >= i) {
                max = Math.max(max, j - i);
                i = lastPos + 1;
            }
            lastPosTable[s.charAt(j)] = j;
            j++;
        }
        return Math.max(max, j - i);
    }
    
    // Method 1: Brute force, time out
    // 2 pointer, characters in the sliding window are distinct 
    // which store in the hashset
    public int lengthOfLongestSubstring1(String s) {
        if (s == null || s.length() == 0)
            return 0;
        HashSet<Character> set = new HashSet<Character>();
        int i = 0, j = 0;
        int max = 0;
        int n = s.length();
        while (j < n) {
            char curr = s.charAt(j);
            if (set.contains(curr)){
                max = Math.max(max, set.size());
                while (i < j && s.charAt(i) != curr) {
                    set.remove(s.charAt(i++));
                }
                set.remove(s.charAt(i++));
            }
            set.add(curr);
            j++;
        }
        return Math.max(max, set.size());
    }
分享到:
评论

相关推荐

    LeetCode3 Longest Substring Without Repeating Characters

    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...

    leetcode答案-LeetCode-Longest_Substring_Without_Repeating_Characters:Leet

    答案LeetCode-Longest_Substring_Without_Repeating_Characters 这是LeetCode上“最长子串无重复字符”问题的解决方案。 问题描述:给定一个字符串,求没有重复字符的最长子串的长度。 示例 1:输入:“abcabcbb” ...

    leetcode题库-leetcode-web:以Web形式展示LeetCode题解,基于PythonFlask

    leetcode题库 LeetCode-Web 初始化 前端库依赖 下载,并将jquery-3.x.x.min.js移动到static目录下。 下载,并将semantic.min.js、semantic.min.css、components和themes...longest-substring-without-repeating-charac

    leetcode2sumc-LeetCode-3.Longest_Substring_Without_Repeating_Characters

    LeetCode-3.Longest_Substring_Without_Repeating_Characters 给定一个字符串,找出没有重复字符的最长子字符串的长度。 示例 1: 输入:“abcabcbb” 输出:3 解释:答案是“abc”,长度为 3。 解决方案 Python3:...

    leetcode题库-LeetCode-Go:用GO回答LeetCode

    leetcode题库 LeetCode-Go 理论基础 见Note 脑图 TODO 待填充 算法题 面试高频出现,以及一些非常经典重要的算法题优先 题目列表 No Title Link Solution Acceptance Difficulty Topics Solution 0001 Two Sum 46.1%...

    javalruleetcode-leetcode-java:力码笔记

    Without Repeating Characters 5.Longest Palindromic Substring 20.Valid Parentheses 26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122....

    华为leetcode-ProgrammingExercises:Leetcode和NewCoder练习

    华为 leetcode mistake-collection This is mistake collection for exercise publoms ...Source2:LeetCode ...Substring Without Repeating Characters code_6.org 二进制链表转整数 code_7.org 最小高度树

    分割数组求最大差值leetcode-Leetcode-Road:LeetCode刷题记录

    分割数组求最大差值leetcode LeetCode 学习之路 记录自己完成LeetCode的代码和结果。 序号 中文名称 英文名称 通过率 难度 1 Two Sum 47.0% 简单 2 Add Two Numbers 36.0% 中等 3 Longest Substring Without ...

    javalruleetcode-LeetCode:LeetCode算法问题

    java lru leetcode Fighting for a job! 记录找工作期间搬运的题,全部使用Java实现,本人C++鶸 1 ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...Characters LeetCode 13 Roman to Integer LeetCode 6 Zi

    dna匹配leetcode-leetcode:leetcode刷题

    Longest Substring Without Repeating Characters 哈希表 双指针 滑动窗口 Substring with Concatenation of All Words 哈希表 注意匹配方向 Valid Sudoku 数组 遍历 Sudoku Solver 深度优先遍历 回溯 先检查后修改 ...

    leetcodepython001-leetcode:https://leetcode.com/的解决方案

    leetcode Python 001 力码 ├── Algorithms │  ├── cpp │  │  ├── 001 - Two Sum.cpp │  │  ├── 002 - Add Two Numbers.cpp │  │  ├── 003 - Longest Substring Without Repeating ...

    gasstationleetcode-leetcode-rust:莱特代码休息

    3-longest-substring-without-repeating-characters 7 cargo run --bin 7-reverse-integer 9 cargo run --bin 9-palindrome-number 13 cargo run --bin 13-roman-to-integer 14 cargo run --bin 14-longest-common-...

    leetcode分类-leetcode:leetcode问题的代码

    leetcode 分类leetcode 问题分类 leetcode代码仓库,我的解题思路写在我的博客里: leetcode 代码库,我博客上的解题思路: mkdir 列表: 大批 #1:Two Sum #4:Median of Two Sorted Arrays 地图 #1:Two Sum #3:...

    leetcode实现strstr-leetcode-js:js刷leetcode

    leetcode实现strstr leetcode-js 记录刷leetcode分析过程,希望一点点进步! leetcode地址 刷题顺序 按照顺序刷第一遍,记录实现思路,自己的优化方案,并研究高票大佬的思路。 已完成题目归档 序号 题目 题解 实现...

    lrucacheleetcode-leetcode-1:leetcode-1

    Without Repeating Characters 最长没有重复字符的子序列 记录各字符最近一次出现的位置 4. Median of Two Sorted Arrays 求两有序数列的中位数,可泛化为求两有序数列的第k位数,二分法 5. Longest Palindromic ...

    leetcode中国-LeetCode-Swift:LeetCode算法题Swift实现

    leetcode中国 LeetCode-Swift LeetCode算法题Swift实现 从2020年6月10日开始挑战每日刷算法,至少完成一道,让我们拭目以待! No. Title Chinese Difficulty Router 0001 Two Sum Easy 0002 Add Two Numbers Medium ...

    leetcode答案-SH_LeetCode:LeetCode-Swift

    leetcode 答案 SH_LeetCode LeetCode-Swift解答记录(Answer records of LeetCode) 11月21日 更新:添加README文件以及LeetCode前三题代码(Add README.md and Code for the first three questions) 1. 两数之和...

    程序员面试宝典LeetCode刷题手册

    3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17...

    LeetCodeTrain:这是来自LeetCode的已解决任务的存储库

    这是来自LeetCode的已解决任务的存储库使用Java语言解决任务 ...repeating-character-replacement/ MergeIntervals.java - //leetcode.com/problems/merge-intervals/ ReverseLinkedList.java - //leetcode.com/problem

    leetcode卡-LeetCode:我的LeetCode解决方案

    leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add Two Numbers], Linked list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡...

Global site tag (gtag.js) - Google Analytics