LeetCode热题3.无重复的最长字串

前言:

经过前序的一系列数据结构和算法学习后,开始用leetCode热题练练手。

. - 力扣(LeetCode)

给定一个字符串 s ,请你找出其中不含有重复字符的最长子串的长度。

示例 1:

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

解题思路:

使用迭代变量字符串,效率还存在优化空间,下期分解。

1.本题使用128的数组存放出现字符的当前位置后一位的下标。

2.当前字符没有存放过则存放并计数。

3.当前字符出现过则计算最大值,并重置i的下标为之前出现重复字符的位置。

小知识点:128个字符ascii码表中最小的是" "空格符。 

实现代码

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int length=s.length();
        int res=0;
        int tempRes=0;
        int[] current=new int[128];
        int i=0;
        for(i=0;i<length;i++){
            //int cur=i;
            if(current[s.charAt(i)-' ']>0){
                res=Math.max(res,tempRes);
                //i置为当前下标,在i++后从之前出现重复的下一位置开始迭代
                i=current[s.charAt(i)-' ']-1;
                current=new int[128];
                tempRes=0;
            }else{
                //为避免放入0下标,放入下一位下标
                current[s.charAt(i)-' ']=i+1;
                tempRes+=1; 
            }  
        }
        res=Math.max(res,tempRes);
        return res;
    }
}

QA1:

相关推荐

  1. LeetCode3.重复

    2024-06-16 22:12:04       33 阅读
  2. LeetCode 3.重复字符发

    2024-06-16 22:12:04       38 阅读
  3. C语言每日一(68)重复字符

    2024-06-16 22:12:04       41 阅读
  4. LeetCodeHot100-重复字符长子

    2024-06-16 22:12:04       46 阅读
  5. 力扣100_滑动窗口_3_重复字符长子

    2024-06-16 22:12:04       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-16 22:12:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-16 22:12:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-16 22:12:04       82 阅读
  4. Python语言-面向对象

    2024-06-16 22:12:04       91 阅读

热门阅读

  1. 在 PHP 中怎样实现实时数据推送功能?

    2024-06-16 22:12:04       30 阅读
  2. 2813. 子序列最大优雅度 Hard

    2024-06-16 22:12:04       31 阅读
  3. springcloud入门与实践

    2024-06-16 22:12:04       24 阅读
  4. Python编程:从入门到实践(第3版)

    2024-06-16 22:12:04       40 阅读
  5. 大厂笔试真题讲解—美团23—小美的蛋糕切割

    2024-06-16 22:12:04       29 阅读
  6. C# 程序结构

    2024-06-16 22:12:04       30 阅读
  7. SQL MAX() 函数深入解析

    2024-06-16 22:12:04       29 阅读
  8. 鸿蒙开发:【PageAbility组件概述+配置】

    2024-06-16 22:12:04       29 阅读