LeetCode刷题笔录 Longest Substring Without Repeating Characters

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 the length of 1. Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring. Java AC版本 立即下载

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.


这题似曾相识,似乎是数据结构课做过的题,不过忘得一干二净。。。。

一开始傻呵呵的用了暴力方法,慢的要死;看到提示了知道可以用一个table来存储每一个字符在字符串中的位置。其实这是一个对于字符串问题的通用方法,因为字符本质上就是一个Unique的数字,因此建立一个数组,数组的下标表示这个字符的ASCII码,元素表示其在字符串中的位置即可。另外一个逻辑就是当遇到和之前重复的字符(前一次出现称为Occur 1, 这一次出现称为Occur 2)时,即遇到了一个可能的最大值;进行比较后,将下一次扫描的起点设置为刚才这个重复字符的Occur 1的后一位即可。看到一张图,觉得很不错,转载过来:



代码如下:

public int lengthOfLongestSubstring(String s) {
        // Start typing your Java solution below
        // DO NOT write main() function
		int length = s.length();
		if (length == 0) {
			return 0;
		}
		int [] countTable = new int[256];
		Arrays.fill(countTable, -1);
		int max = 1;
		int start = 0;
		int end = 1;
		
		countTable[s.charAt(0)] = 0;
		while (end < length) {
			//Has not reached a duplicate char
			if (countTable[s.charAt(end)] >= start) {
				start = countTable[s.charAt(end)] + 1;				
			}
			max = Math.max(max, end - start + 1);
			countTable[s.charAt(end)] = end;
			end++;
		}
		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. 立即下载

相关推荐

sentaurus学习笔记(VDMOS仿真)(三)

在Sentaurus中,这通常意味着在2D仿真中,x轴指向下,y轴指向右。定义了在2D模拟中,网格尺寸按照一定的增长率进行扩展,这里的增长率为2.0,意味着远离最小尺寸区域的网格单元大小将是最小尺寸的两倍。定义了2D模拟中网格单元的最小尺寸为0.001微米,这有助于控制网格的最小大小,从而影响仿真的精度和计算成本。表示启用中等级别的日志信息输出,这有助于用户了解仿真过程中发生的事件和可能的问题。表示启用保存调试文件,这可以帮助用户在仿真过程中检查和验证模型的状态。这行代码设置了仿真过程中使用的线程数。

qq_52640792的博客 1807

3. Longest Substring Without Repeating Characters题目和答案详解

1 题目简述 Given a string, find the length of the longest substring withoutrepeating characters. 给定一个字符串,找到最长的没有重复字符的子串。 Examples: Given "abcabcbb", theanswer is "abc", which the lengthis 3. 给定

Disappear_XueChao的博客 431

【UV打印机】PrintExp打印软件教程(二)-主界面介绍

PrintExp软件主界面如下图所示PrintExp软件的主界面主要包括主菜单区,工具栏区,界面显示区,状态栏。以下是上述部分的介绍。

Print World的专栏 6299

LeetCode第三题---Longest Substring Without Repeating Characters (c++)

每周坚持刷博客,天道酬勤,加油,共勉! 题目: Given a string, find the length of the longest substring without repeating characters. Example 1: Input: “abcabcbb” Output: 3 Explanation: The answer is “abc”, with the length ...

WangHuanToBeBest 326

从AND到XOR:5种门控电路在PrimeTime中的检查策略差异

本文深入探讨了AND、NAND、OR、NOR和XOR五种门控电路在PrimeTime静态时序分析中的检查策略差异。重点解析了XOR门控因其非单调性而无法被工具自动推断,需要工程师手动配置的特殊性,并提供了针对各类门控的配置模板与调试技巧,以确保时钟网络的完整性与芯片设计的可靠性。

rgv2345678的博客 269

EMC工程师职业发展:从测试整改到系统设计的进阶之路

电磁兼容(EMC)是确保电子设备在复杂电磁环境中可靠工作的关键技术,其核心在于控制电磁干扰(EMI)并提升抗扰度。其基本原理涉及电磁场理论、噪声耦合路径与滤波屏蔽技术。对于电子产品而言,EMC设计直接关系到能否通过CE、FCC等强制性认证,是产品上市和市场竞争力的关键。在工程实践中,EMC工程师需要从电路设计、PCB布局、元器件选型等源头入手,将EMC要求融入产品开发全流程,实现从后期“整改”到前期“设计预防”的价值跃迁。本文聚焦EMC工程师的核心能力体系构建与职业发展路径,深入探讨了如何从掌握标准测试、电

weixin_30819085的博客 387

LeetCode-3-无重复字符的最长子串(longest-substring-without-repeating-characters)

题目描述 Given a string, find the length of the longest substring without repeating characters. 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 示例 2: 输入: "b...

玩大神的优快云的博客 341

Fluent UDF实战:除了速度入口,你的DEFINE_PROFILE宏还能搞定这些边界条件(温度、组分、壁面接触角全解析)

本文深入解析Fluent UDF中DEFINE_PROFILE宏在复杂边界条件中的高阶应用,涵盖温度场、组分浓度和壁面接触角等关键场景。通过实战代码示例,展示如何实现空间梯度温度场、动态组分控制和接触角动态调整,助力CFD工程师突破标准界面限制,提升仿真精度和效率。

weixin_28688385的博客 448

【LeetCode 面试经典150题】3. Longest Substring Without Repeating Characters 无重复字符的最长字串

s。

qq_43513394的博客 640

OpenStack详解、排错、经验总结

第1章 Openstack简介 1.1 计算(Compute):Nova。 一套控制器,用于为单个用户或使用群组管理虚拟机实例的整个生命周期,根据用户需求来提供虚拟服务。负责虚拟机创建、开机、关机、挂起、暂停、调整、迁移、重启、销毁等操作,配置CPU、内存等信息规格。自Austin版本集成到项目中。 1.2 对象存储(Object Storage):Swift。 一套用于在大规模可扩展系统中通过内置冗余及高容错机制实现对象存储的系统,允许进行存储或者检索文件。可为Glance提供镜像存储,为Cinder提供

weixin_40548182的博客 1974

【python数据处理】python实现vlookup字符串模糊匹配及在实战中的应用(FuzzyWuzzy库)

利用FuzzyWuzzy库匹配字符串1. 背景前言2. FuzzyWuzzy库介绍2.1 安装2.1 fuzz模块2.1.1 简单匹配(Ratio)2.1.2 非完全匹配(Partial Ratio)2.1.3 忽略顺序匹配(Token Sort Ratio)2.1.4 去重子集匹配(Token Set Ratio)2.2 process模块2.2.1 extract提取多条数据2.2.2 extractOne提取一条数据3. pandas实战应用3.1 公司名称字段模糊匹配3.1.1 参数讲解:3.1.2

lys_828的博客 8321

LeetCode:Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters Medium Given a string, find the length of thelongest substringwithout repeating characters. Example 1: Input: "abcabcbb"Output: 3 Explanation: Th...

啃梨的博客 266

手把手教你学pcie--用Verilog搭建PCIe控制器核心模块

PCIe控制器是硬件设计的核心模块,负责实现设备与主机之间的高速通信。本文将以Verilog语言为例,分步骤讲解如何从零构建一个简化的PCIe控制器核心模块,涵盖事务层(TLP)生成、数据链路层(DLLP)处理和物理层(PHY)接口适配。目标是为嵌入式开发者、FPGA工程师提供一个可扩展的硬件设计框架。​。

MHD0815的博客 339

Given a string, find the length of the 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 the le

简国堂的博客 1418

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Given a string, find the length of the 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 the l...

lifeqiuzhi521的博客 576

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

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.

[&] () {} 1万+

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

【LeetCode】3. Longest Substring Without Repeating Characters 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode....

负雪明烛 1万+

Leetcode 3. Longest Substring Without Repeating Characters (滑动窗口解法)

话说我有一个小习惯,我会去看一下 Debugu code in playground, 我觉得它的题目的输入输出的代码都写的特别的棒。Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc",...

qq_25175067的博客 396

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

wl_ss的博客 2688

LeetCode 3.Longest Substring Without Repeating Characters c语言版

问题描述: 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

banana1006034246的博客 2352

lengthOfLongestSubstring

LeetCode 链接 : https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/description/  题目: 无重复字符的最长子串给定一个字符串,找出不含有重复字符的 最长子串 的长度。示例:给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3。给定 "...

wenke8619的博客 1851

LeetCode 3. 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 "

老夫编代码就是一把梭 631

LeetCode3.无重复字符的最长子串lengthOfLongestSubstring(哈希表+滑动窗口)

1、题目描述 2、代码详解 class Solution { public int lengthOfLongestSubstring(String s) { int[] last = new int[128]; Arrays.fill(last, -1); int start = 0; int ans = 0; for (int i...

IOT_victor的博客 758

3. 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 "

Alan_Xiang的博客 1245

3.lengthOfLongestSubstring

题目: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 i

AlphaGQ 1332
上一篇: LeetCode刷题笔录 Median Of Two Sorted Arrays
下一篇: Stanford Algorithms学习:Clustering
likecool21
博客等级 码龄14年 14粉丝 123原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值