`

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

阅读更多

 

     思路:

     比如 : "dvdfzxd"字符串,我要求他的最长无重复字符的子串。

     可以知道的,一定要从开头遍历到结尾。

     这样,从第一个开始,一直读,直到最后一个,如果读到的字符与之前的重复了,

     那么前面部分就可以看成一个符合要求的子串,记录它的长度。那么接下来就是跳过

     刚重复的字符,以它的下一个节点为起点,重新计算一个新的子串的长度。

     比如这里,遍历到第3个元素d时,与第一个d重复,那么记录,子串长度2,从v开始

     就算新的子串的长度。

     

 

    思路是这样,但是怎么知道之前的字符重复过了?

    HashMap能够很好的帮我们解决。

    用它来存储<字符,小标> 。

 

    但是,"abba"这种情况,该怎么解决了?

   当我们读到第2个a是,明显他们之间还有其他的重复字符。

   可以通过设置一个起点变量,来表示现在计算的子串的起点,同时如果重复,则每次都更新为

    最新的小标。

 

    综上,得到代码:

   

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        HashMap<Character , Integer> map = new HashMap<> ();
        int sum = 0;
        int max = 0;
        int start = 0;
        for(int i = 0 ; i < s.length() ; i++) {
            char c = s.charAt(i);
            if(map.containsKey(c)) {
                int idx = map.get(c);
                //如果重复的元素在新子串内
                if(idx >= start) {
                    //更新为最新的下标
                    map.put(c,i);
                    //最新的无重复元素的子串则是去掉第一个相同的元素
                    sum = i - idx;
                    //新子串的起点
                    start = idx + 1;
                } else {
                    //更新节点为最新的位置
                    map.put(c ,i);
                    sum++;
                } 
            } else {
                map.put(c , i);
                sum++;
            }
            max = Math.max(sum , max);
        }
        return max;
    }
}

 

分享到:
评论

相关推荐

    没有重复字符最长子串

    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.

    longest-substring-without-repeating-characters:无重复字符的最长子串的长度(http

    最长子串无重复字符没有重复字符的最长子串的长度( )

    leetcode #3 无重复字符的最长子串(C)

    解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters    一上来没啥思路,好久不做题...

    javalruleetcode-Leetcode:力码解决方案

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

    无重复字符的最大子串

    链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 思路 构造一个辅助字符串 sub ,运用字符串的 find 函数判断当前字符是否存在于辅助字符串 sub 中,如果不存在则添加进去...

    lrucacheleetcode-leetcode-1:leetcode-1

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

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

    无重复字符的最长子串 string 4 LMedian of Two Sorted Arrays 两个排序数组的中位数 ary,binary search,dive and conquer 5 Longest Palindromic Substring 最长回文子串 string,dp 8 String to Integer(atoi) 字符...

    leetcode跳跃-LeCode:乐科

    无重复字符的最长子串 4. Median of Two Sorted Arrays 寻找两个有序数组的中位数 5. Longest Palindromic Substring 最长回文子串 6. ZigZag Conversion Z字型变换 7. Reverse Integer 整数反转 8. String to ...

    leetcode答案-LeetCode-Longest_Substring_Without_Repeating_Characters:Leet

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

    leetcode跳跃-leetcode:leetcode一天一次

    无重复字符的最长子串:3. Longest Substring Without Repeating Characters - 最小跳跃步数问题 - 动态规划 + 循环策略优化:45. Jump Game II - 二叉树 前序遍历判断二叉树:98. Validate Binary Search Tree - 二...

    leetcode题库-LeetCode:力码

    无重复字符的最长字串 Longest Substring Without Repeating Characters.cpp 4 寻找两个有序数组的中位数 Median of Two Sorted Arrays.cpp 5 最长回文子串 Longest Palindromic Substring.cpp 7 整数反转 Reverse ...

    leetcode分类-Leetcode:leetcode

    最长的不出现重复字符的子串。 典型的two points问题,枚举左端点,维护右端点的右移。用个数组记录一下字符出现个数就ok了。O(n)。 不了解two points的话,也很容易想到具有单调性,枚举左端点,二分右端点,维护...

    leetcode下载-lengthOfLongestSubstring:使用webpack打包一个公共基础包,并且把包发到npm

    https://leetcode-cn.com/problems/longest-substring-without-repeating-characters 下面开始一步步介绍: 新建项目目录:mkdir lengthOfLongestSubstring 进入目录:cd lengthOfLongestSubstring 初始化

    leetcode答案-SH_LeetCode:LeetCode-Swift

    leetcode 答案 SH_LeetCode LeetCode-Swift解答记录(Answer records of LeetCode) 11月21日 更新:添加README文件以及LeetCode前三题代码...无重复字符的最长子串(Longest Substring Without Repeating Characters)

    leetcode双人赛-Strings_Algorithms_C-:用C++编写的字符串算法

    :包含问题的解决方案:给定一个字符串,找到没有重复字符的最长子串的长度。 ****************************************************** ********** is_anagram_O(n).cpp :包含O(n) 时间复杂度问题的解决方案:给定...

    acm和leetcode难度-leetcode:leetcode算法分析和代码实现

    acm和leetcode难度 leetcode 解题列表 俗话说: 熟读唐诗三百首, ...没有重复字母的最长子串 [Longest Substring Without Repeating Characters] ★★☆☆☆ 字符串 / 判重 4 两个有序数组的中值 [Median of Two So

    leetcode数组下标大于间距-LeetCode:力扣GoLang

    longest-substring-without-repeating-characters: 最长不重复子串 最长不重复子串(获取长度) 使用一个可截取的字符串,或者队列(FIFO)来遍历一遍原字符串,当有字母重复的时候,开始从头删,一直到删除重复的元素,然后...

    leetcode数组下标大于间距-LeetCode_Solutions::party_popper:我的力扣解决方案

    结尾的子串,内层循环根据st[i]检查是否有重复。这种方法可以将时间复杂度从 O(n^3) 降低到 O(n^2)。 0004. Median of Two Sorted Arrays 解题思路 二分查找,并且使用了Median的性质。对于A的划分i,表示以第i - 1...

    leetcode296-Algorithm:来自https://github.com/xidui/algorithm-training的算法演

    [无重复字符的最长子串](#Longest-Substring-Without Repeating-Characters) [ZigZag 转换](#ZigZag 转换) [反向整数](#反向整数) [回文数](#回文数) 二和 给定一个整数数组,找到两个数字,使它们相加为特定的目标...

    leetcode2sumc-leetcode:leetcode

    [python](./Longest_Substring_Without_Repeating_Characters.py) 中等的 4 [两个有序数组的中位数]() [python](./Median_of_Two_Sorted_Arrays.py) 难的 5 [最长回文子串]() [python](./Longest_Palindr

Global site tag (gtag.js) - Google Analytics