`
frank-liu
  • 浏览: 1665536 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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.

原问题链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/

 

问题分析

    这个问题看起来还是比较简单的。需要找一个字符串里最大不包含重复元素的子串。我们可以根据一些串的示例来分析一下:

     假定我们有如下的一个字符串:

      

     我们最初的想法可以是这样,通过将每个字符和它所在的索引保存到一个map里,每次将一个元素加入到map的时候事先判断一下map里是否存在该字符,如果存在则发现有重复的元素,需要做一些处理。否则直接将该元素和它所在的索引加入到map中。既然要取得最大的子串长度,我们需要有一个变量来记录当前串的起始位置以及当前的结束位置。

     在上述的示例中,假设分别以i, j表示当前串的开始和结束。当碰到一个重复的元素时,其情景如下图:

     

     这个时候我们该怎么办呢?为了计算最大的子串,我们应该将起始点移到这个已经存在的这个节点后面。也就是b这个位置上。也就是如下图这样:

    

    这个时候,我们需要更新原来那个重复元素映射的新位置,也就是将a映射的值设置为j。当然,因为前面这个示例有一点特殊,还有一个地方我们忽略了。就是假设i这个元素它并不是后面找到重复的地方,而是在它稍微后面一点的地方,比如下图这样:

    

    这个时候,我们碰到重复元素b,应该将i移到b后面。但是这个时候我们也应该修改map。因为map里应该只保存从i到j之间的所有元素,所以从i到b之间的所有元素应该被移除掉。

    这样,经过这些讨论我们就有了这么一个思路。首先记录起始和终止标识i, j为0。然后j开始向后遍历,每次遍历就检查是否在map里存在该元素。如果不存在则直接加入到map中。如果存在,则从i到它找到存在的那个元素之间将这些元素从map里去掉,然后设置i为重复元素后面的那个位置,再更新冲突的位置为j。这样可以得到如下的代码:

 

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        if(s == null || s.length() == 0) return 0;
        int len = 0;
        Map<Character, Integer> map = new HashMap<>();
        for(int i = 0, j = 0; j < s.length(); j++) {
            Character c = s.charAt(j);
            if(map.containsKey(c)) {
                int pos = map.get(c);
                for(int k = i; k <= pos; k++) map.remove(s.charAt(k));
                i = pos + 1;
            }
            map.put(c, j);
            len = Math.max(len, j - i + 1);
        }
        return len;
    }
}

     总体来说,这个解法的时间复杂度为O(n)。

 

总结

    这个问题并不复杂,主要是需要细心的分析一下就可以了。把图画出来就好办。

 

  • 大小: 4.1 KB
  • 大小: 5.5 KB
  • 大小: 5.4 KB
  • 大小: 5.4 KB
分享到:
评论

相关推荐

    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:leetcode问题的代码

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

    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

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

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

    leetcode2sum-Problems:编程问题的回购

    leetcode 2sum # Programming-Problems This will have many problems from all over the web, As of writing this readme there is Haskell 99: 28 finished + Huffman Coding LeetCode: LC1: 2Sum [Easy] LC2: Add...

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

    leetcode答案-LeetCode-Longest_Substring_Without_Repeating_Characters:Leet

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

    java猜字母游戏源码-leetcode:leetcode记录

    #leetcode 001_Medium: Two Sum.利用哈希表(在O(1)时间复杂度内查找某个元素)。遍历数组,查找map里是否containsKey(target-nums[i]),如果则返回,否则将nums[i]放入map中(map.put(nums[i],i)。 002_Medium: Add ...

    leetcode跳跃-leetcode:leetcode一天一次

    leetcode 跳跃 leetcode 动态规划,背包问题 01背包问题:416. Partition Equal Subset Sum 最长回文:5. Longest Palindromic Substring - 字数组余数:523. Continuous Subarray Sum - 无重复字符的最长子串:3. ...

    leetcode2-LeetCode:LeetCode刷题记录,C++挑战所有题目的前2个柱子分布(以前是前10%,发现太看运气了,囧)

    leetcode 2 #LeetCode 解题记录 ##开始时间 2016年1月14日晚 ##目标 使用C++和python完成所有题目,并且排名全部在90%之前。 ###2016年1月14日晚 ####题号1: two sum C++: 击败 97.85% python : 未完成 ###2016年...

    leetcode分类-leetcode:leetcode

    leetcode 分类 Leetcode Introduction 本文旨在将leetcode的题型分类 Pattern: Sliding window 滑动窗口类型的题目经常是用来执行数组或是链表上某个区间(窗口)上的操作。比如找最长的全为1的子数组长度。滑动窗口...

    lrucacheleetcode-leetcode:leetcode

    lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. ...

    Leetcode的ac是什么意思-LeetCode:练习LeetCode

    Leetcode的ac是什么意思 LeetCode解题 第一题:Two Sum 这题是给你一个整数的数组,然后给你一个目标值,让你从这个数组中找出两个数相加等于这个目标值的数组索引值(不能是同一个元素)。 第一种解题思路就是两个...

    dna匹配leetcode-leetcode:leetcode刷题

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

    leetcode双人赛-LeetCode:力扣笔记

    leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...

    leetcode338-LeetCode:LeetCode刷题总结

    LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....

    leetcode中文版-LeetCode:LeetcodeC++/Java

    leetcode中文版 LeetCode # Title Chinese Tag Solution 1 Two Sum 两数之和 array,hash 2 Add Two Numbers 两数相加 math 3 Longest Substring Without Repeating Characters 无重复字符的最长子串 string 4 ...

    javalruleetcode-Leetcode:力码解决方案

    Repeating Characters (无重复字符的最长子串) Medium Dual Pointer (Sliding Window), Hash Table 4 Median of Two Sorted Arrays (寻找两个有序数组的中位数) Hard Divide and Conquer 5 Longest Palindromic ...

    leetcode题库-LeetCode:力码

    leetcode题库 LeetCode 题解合集 本仓库展示了LeetCode题库中部分题目的解法(持续更新),所有代码均采用C++编写并配有输入输出样例 代码清单如下: 序号 题目 程序源码 1 两数之和 Two Sum.cpp 2 两数相加 Add Two...

    leetcode2sumc-Leetcode:记录我的大学Leetcode之旅。争取寒假能够整理完刷过的350题

    leetcode 2 sum c Leetcode 记录我的Leetcode之旅 题号 难度 知识点 完成情况 1. Two Sum Easy 搜索 :check_mark_button: 2. Add Two Numbers Medium 链表 :check_mark_button: 3. Longest Substring Without ...

Global site tag (gtag.js) - Google Analytics