当前位置:网站首页>[sword finger offer] 48 Longest substring without duplicate characters
[sword finger offer] 48 Longest substring without duplicate characters
2022-06-27 23:25:00 【LuZhouShiLi】
The finger of the sword Offer 48. The longest substring without repeating characters
subject
Please find the longest substring in the string that does not contain duplicate characters , Calculate the length of the longest substring .
Ideas
Set the status array dp,dp[j] Represents with the character s[j] For the end of “ The longest non repeating substring ” The length of
Transfer equation : Fixed right border j, Set character s[j] The same character nearest to the left is s[i], namely s[i] = s[j]
When i < 0, namely s[j] There is no same character on the left , be dp[j] = dp[j - 1] + 1; When dp[j - 1] < j - i, Description character s[i] In substring dp[j - 1] Out of range , be dp[j] = dp[j - 1] + 1; When dp[j - 1] >= j - i, Description character s[i] In string dp[j - 1] Within the interval , be dp[j] The left boundary of is bounded by s[i] decision , namely dp[j] = j - i;
return max(dp)
Code
class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character,Integer> dic = new HashMap<>();// Define a hash table
int res = 0,tmp = 0;
for(int j = 0; j < s.length(); j++)
{
int i = dic.getOrDefault(s.charAt(j),-1);// s.charAt(j) Gets the character of the current position , Then look up the position of the last occurrence of the character from the hash table
// Store the character position in the hash table
dic.put(s.charAt(j),j);// Update hash table
// Calculate the difference between the current position and the last position and tmp(tmp Is the longest substring length of the previous character ) Compare
// Here, the longest substring length of the current character is updated
if(tmp < j -i)
{
tmp = tmp + 1;
}
else
{
tmp = j - i;
}
// to update res
res = Math.max(res,tmp);// max(dp[j - 1],dp[j])
}
return res;
}
}
边栏推荐
- Is it safe for GF futures to open an account?
- Livox lidar+ Haikang camera generates color point cloud in real time
- 【蓝桥杯集训100题】scratch数字计算 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第16题
- Classification of cifar-10 dataset with pytorch
- Livox Lidar+APX15 实时高精度雷达建图复现整理
- "Top stream Aidou manufacturing machine" cooperates with four industrial capitals to become LP
- [js]var, let,const 的区别
- 【剑指Offer】47. 礼物的最大价值
- 【你真的会用ES吗】ES基础介绍(二)
- Using xgboost with tidymodels
猜你喜欢

【剑指Offer】47. 礼物的最大价值

Feign implements path escape through custom annotations

golang - new和make的区别

Feign通过自定义注解实现路径的转义

MSP430F5529 单片机 读取 GY-906 红外温度传感器

【蓝桥杯集训100题】scratch数字计算 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第16题

Ice cream or snow "high"?

EXCEL 打印设置公共表头

Redis principle - string

How to set the enterprise wechat group robots to send messages regularly?
随机推荐
未能加载文件或程序集“CefSharp.Core.Runtime.dll”或它的某一个依赖项。 不是有效的 Win32 应用程序。 (异常来自 HRESULT:0x800700C1)
实践torch.fx:基于Pytorch的模型优化量化神器
Is it safe for GF futures to open an account?
EXCEL 打印设置公共表头
The National University of Singapore 𞓜 uses model free reinforcement learning to evaluate the energy efficiency of the energy efficiency data center
跟着存档教程动手学RNAseq分析(二)
Learn rnaseq analysis by following the archiving tutorial (I)
vivado 如何添加时序约束
Death of 5 yuan youkuang in Yuanqi forest
This year's examinees are more "desperate" than the college entrance examination
小程序referer
Feign通过自定义注解实现路径的转义
Spark BUG实践(包含的BUG:ClassCastException;ConnectException;NoClassDefFoundError;RuntimeExceptio等。。。。)
First principles (optimal solution theory)
通过tidymodels使用XGBOOST
Summary of various loams (laser SLAM)
【微服务|Sentinel】sentinel数据持久化
Ice cream or snow "high"?
Practice torch FX: pytorch based model optimization quantization artifact
Livox Lidar+海康Camera 基于loam的实时三维重建生成RGB彩色点云