LeetCode!! 3. Longest Substring Without Repeating Characters

这篇具有很好参考价值的文章主要介绍了LeetCode!! 3. Longest Substring Without Repeating Characters。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

参考资料:左程云算法课

3. Longest Substring Without Repeating Characters
Given a string s, find the length of the longest substring without repeating characters.

 

Example 1:

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

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

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
 

Constraints:

0 <= s.length <= 5 * 104
s consists of English letters, digits, symbols and spaces.

思路:
首先,以str[i]为例,计算 局部最优解 以str[i]为结尾的无重复字符的最长子串的长度。然后,遍历字符串,即i取0,1,…, n-1,取局部最优解的最大值,得到全局最优解。
具体地,求 以str[i]为结尾的无重复字符的最长子串的长度,可以使用动态规划的方法。
dp[i]表示 以str[i]为结尾的无重复字符的最长子串的长度
以str[i]为结尾的无重复字符的子串不停地向左扩,直到扩不动了为止。扩不动的原因有两个:
第一,碰到了与str[i]相同的字符;
第二,碰到了dp[i-1]的局部最优解对应的开头的前一个位置,也就是令dp[i-1]扩不动的地方。
上面两个位置取最大,就是 令 以str[i]为结尾的无重复字符的子串 向左扩不下去的地方。此时,可以求得dp[i]。

一些实现细节:
使用数组map记录,遍历到i字符时,之前的所有字符最近出现过的位置。
注意:“还没出现过”的位置不能使用默认值0,而是指定为-1.
这是因为,‘0’ 已经有自己的含义了,如map['a']=0表示字符’a’最近一次出现是在0位置。文章来源地址https://www.toymoban.com/news/detail-480345.html

// 写法1:有dp表的动态规划
 public int lengthOfLongestSubstring3(String s) 
    {
        if(s==null || s.length()==0){
            return 0;
        }

        char[] chs = s.toCharArray();
        int n=chs.length;
        int[] dp = new int[n];
        dp[0]=1;
        int[] map = new int[256];
        for(int i=0;i<256;i++){
            map[i]=-1;
        }
        map[chs[0]]=0;
        int res=1;
        for(int i=1;i<n;i++){
            dp[i] = i-Math.max(map[chs[i]],i-1-dp[i-1]);// Math.max(map[chs[i]],i-1-dp[i-1])是 局部最优开头的前一位的坐标
            if(dp[i]>res){res=dp[i];}
            map[chs[i]]=i;
          
        }
        return res;

    }

// 写法2:省去了dp表,只使用有限几个变量,因为dp[i]在dp表中只依赖dp[i-1]
     public int lengthOfLongestSubstring(String s) 
    {
        if(s==null || s.length()==0){
            return 0;
        }
        char[] str = s.toCharArray();
        int n = str.length;
        int[] map = new int[256];
        for(int i=0;i<n;i++){
            map[str[i]]=-1;
        }
        int pre = -1;
        int cur=1;
        int res=1;
        map[str[0]]=0;
        for(int i=1;i<n;i++){
            pre = Math.max(pre,map[str[i]]);
            cur = i-pre;
            res = cur>res?cur:res;
            map[str[i]]=i;
        }
        return res;
    }

// 写法2: 和上面一样的写法,这里有些注释,可供参考
    public int lengthOfLongestSubstring2(String s) 
    {
        if(s==null || s.length()==0)
        {
            return 0;
        }
        char[] str = s.toCharArray();
        int[] map=new int[256];// <char,position>
        for(int i=0;i<256;i++)
        {
            map[i]=-1; 
            // 因为‘0’已经有含义了,它表示 the first index
            // 所以我们选择另外一种数字表示 “还没出现过”
        }
        // LSs who ends up with [i], 
        //  pre : dp[i-1]'s previous index of its locally optimal start
        int pre = -1;
        int cur = 0;// local opt
        int ans=cur;// global opt
        for(int i=0;i<str.length;i++)
        {
            pre = Math.max(pre,map[str[i]]);
            cur = i-pre;
            map[str[i]]=i;
            ans = Math.max(ans,cur);
        }
      return ans;
    }

到了这里,关于LeetCode!! 3. Longest Substring Without Repeating Characters的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode1124. Longest Well-Performing Interval

    We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of

    2024年01月21日
    浏览(26)
  • 【递增栈】个人练习-Leetcode-845. Longest Mountain in Array

    题目链接:https://leetcode.cn/problems/longest-mountain-in-array/ 题目大意:给出一个数组 arr[] ,定义【山数组】为【长度大于等于3】【前面部分递增,后面部分递减,存在一个山峰元素】的数组。求 arr[] 中的最长的是【山数组】的子列。 思路:递增栈当然可以,不过刚好想到另一个

    2024年02月06日
    浏览(42)
  • LeetCode 1048. Longest String Chain【记忆化搜索,动态规划,哈希表,字符串】中等

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月05日
    浏览(33)
  • 【实战】流动的箭头 —— 线性流动组件(repeating-linear-gradient,@keyFrames)

    在大屏数据展示中,若是按节点展示在不同数据层的数据时,为了形象体现数据的流动效果,需要让节点之间,层与层之间用 流动的虚线+箭头 连接。 正常情况下,数据需要从上个节点底部出发到下个节点顶部结束,层与层之间传递需要经过汇总。因此水平方向上主要有两种

    2024年02月08日
    浏览(25)
  • 687. Longest Univalue Path

    687. Longest Univalue Path l,r别写错

    2024年01月19日
    浏览(36)
  • 题解:ABC320B - Longest Palindrome

    链接:Atcoder。 链接:洛谷。 算法难度:C。 思维难度:C。 调码难度:C。 综合评价:入门。 字符串处理。 通过双层循环分别枚举第一个字符和最后一个字符遍历每个子串,在分别判断是否为回文串,在所有是回文串的里面取长度最大值。 O(|s|2)。 字符串截取用substr函数。

    2024年02月07日
    浏览(33)
  • php报错:Malformed UTF-8 characters, possibly incorrectly encoded

    \\\"Malformed UTF-8 characters, possibly incorrectly encoded\\\" 这个错误通常会在处理含有非UTF-8字符的数据时出现,尤其是在使用 json_encode() 函数时。 这可能是由于你的数据包含了非UTF-8字符,而 json_encode() 函数需要转换为UTF-8编码的数据。以下是一些解决此问题的方式: 确保所有输入的数据

    2024年02月09日
    浏览(39)
  • 最长递增子序列(Longest Increasing Subsequence)-C语言实现

    前言 最长递增子序列属于经典的动态规划问题,属于约束条件下求最大子集的问题。这里的约束条件意思是,子序列需要严格按照递增条件产生,在这个前提之下,我们要求到最长的子序列。这类问题的衍生问题有很多,其本质都是穷举最大化子集。 问题描述 问题其实非常

    2024年02月02日
    浏览(43)
  • uniapp 微信小程序 姓名脱敏 substring报错问题:Cannot read property ‘substring‘ of undefined

    效果图: 刘德华----------刘* 加v-if判断是因为如果是后台数据返回的字段,如果不加判断,substring的时候有可能数据还没渲染完,会报错

    2024年02月15日
    浏览(33)
  • MySQL && PostgreSQL截取substring

    SUBSTRING( string ,  start ,  length )  OR  SUBSTRING( string  FROM  start  FOR  length ) 或者 Left

    2024年01月21日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包