博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
剑指 Offer 48. 最长不含重复字符的子字符串
阅读量:4034 次
发布时间:2019-05-24

本文共 1234 字,大约阅读时间需要 4 分钟。

题目描述

请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。

示例 1:

输入: “abcabcbb”

输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
示例 2:

输入: “bbbbb”

输出: 1
解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。
示例 3:

输入: “pwwkew”

输出: 3
解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。
请注意,你的答案必须是 子串 的长度,“pwke” 是一个子序列,不是子串。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Java

class Solution {
//动态规划和模拟哈希表 public int lengthOfLongestSubstring(String s) {
//用数组代替哈希表存储出现的字符上次出现的位置,初始化值为-1 if(s.length()<2) return s.length(); int position[]=new int [256]; //剑指offer书上是26个字母, 但是这里是ASCII上的256个字符 for(int i=0;i
curlength) curlength++; else{
if(curlength>maxlength) maxlength=curlength; curlength=i-preIndex; } position[(int)s.charAt(i)]=i; } if(curlength>maxlength) maxlength=curlength; return maxlength; } }

//滑动窗口法

class Solution {
//滑动窗口,用Set维护一个不重复的窗口 public int lengthOfLongestSubstring(String s) {
int res=0; Set
set=new HashSet<>(); for(int l=0,r=0;r
你可能感兴趣的文章
Eclipse安装FindBugs插件
查看>>
联想T440怎么把原装Win8或Win10换成Win7系统
查看>>
PowerDesigner将物理数据模型图生成图片
查看>>
PowerDesigner创建物理数据模型(PDM)
查看>>
PowerDesigner:导入SQL脚本
查看>>
PowerDesigner使用教程
查看>>
eclipse中使用Ctrl+Alt+↑或↓时屏幕旋转的问题
查看>>
freemarker 数字格式化(金额格式化)
查看>>
eclipse中Deployment Assembly选项设置说明
查看>>
maven项目报:Project configuration is not up-to-date with pom.xml. Run Maven->Update Project
查看>>
pom.xml中maven-compiler-plugin插件配置的使用
查看>>
使用maven-war-plugin 对Maven项目进行动态打包
查看>>
spring定时任务配置
查看>>
Log4j2 配置笔记(Eclipse+maven+SpringMVC)
查看>>
java设计模式之简单工厂模式
查看>>
struts2中constant参数设置
查看>>
Struts2中struts.multipart.maxSize设置
查看>>
CheckStyle插件在eclipse中的安装及配置
查看>>
PowerDesigner 导入数据库建表SQL脚本生成物理模型
查看>>
idea的xml配置中url显示:URI is not registered ( Setting | Project Settings | Schemas and DTDs )
查看>>