Longest Substring Without Repeating Characters

本文详细解析了寻找字符串中最长无重复字符子串的算法实现,通过具体示例和代码片段,展示了如何利用字符映射和滑动窗口技术高效解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

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

 

解答:

 1 public class Solution {
 2 
 3     public static void main(String[] args) {
 4         String s = "abca";
 5         System.out.println(s);
 6     }
 7 
 8     public int lengthOfLongestSubstring(String s) {
 9         int[] charMap = new int[256];
10         Arrays.fill(charMap, -1);
11 
12         int i = 0;
13         int maxLen = 0;
14         for(int j = 0; j < s.length(); j++) {
15             if(charMap[s.charAt(j)] >= i) {
16                 i = charMap[s.charAt(j)] + 1;
17             }
18 
19             charMap[s.charAt(j)] = j;
20             maxLen = Math.max(j-i+1, maxLen);
21         }
22 
23         return maxLen;
24     }
25 }

 

转载于:https://www.cnblogs.com/wylwyl/p/10388840.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值