当前位置:网站首页>[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;
}
}
边栏推荐
- EasyCVR平台路由日志功能的技术实现过程【附代码】
- 「R」 Using ggpolar to draw survival association network diagram
- This kind of people began to be robbed by VC with a monthly salary of 80000 yuan
- [Blue Bridge Cup training 100 questions] scratch digital calculation Blue Bridge Cup competition special prediction programming question collective training simulation exercise question No. 16
- OData - API using SAP API hub in SAP S4 op
- pytorch实现kaggle猫狗识别
- Feign implements path escape through custom annotations
- Batch processing - Excel import template 1.1- support multiple sheet pages
- "Top stream Aidou manufacturing machine" cooperates with four industrial capitals to become LP
- Is the dog virtue training with a monthly salary of 30000 a good business?
猜你喜欢

PE buys a underwear company

Detect objects and transfer images through mqtt

seata

Livox lidar+ Hikvision camera real-time 3D reconstruction based on loam to generate RGB color point cloud

Livox lidar+apx15 real-time high-precision radar map reproduction and sorting

Spark BUG實踐(包含的BUG:ClassCastException;ConnectException;NoClassDefFoundError;RuntimeExceptio等。。。。)

Spark bug Practice (including bug: classcastexception; connectexception; noclassdeffounderror; runtimeexceptio, etc.)

Feign implements path escape through custom annotations

Design of STM32 and rc522 simple bus card system

Liuleifeng, a "good man in Guangzhou" in the first quarter of 2022, has a strong sense of integrity and food safety
随机推荐
Hiplot 在线绘图工具的本地运行/开发库开源
Open source of local run / development library of hiplot online drawing tool
How to set the enterprise wechat group robots to send messages regularly?
Azure Kinect DK realizes 3D reconstruction (PC non real time version)
消除el-image图片周围间隙
PE buys a underwear company
[essay]me53n add button to call URL
[electron] basic learning
批量处理-Excel导入模板1.1-支持多Sheet页
Small chip chiplet Technology
居家办公竟比去公司上班还累?
OData - SAP S4 OP 中使用SAP API Hub 的API
移动端避免使用100vh[通俗易懂]
Livox Lidar+APX15 实时高精度雷达建图复现整理
最新云开发微信余额充电器特效小程序源码
Design of STM32 and rc522 simple bus card system
The latest cloud development wechat balance charger special effect applet source code
Aggregation and index optimization of mongodb basic operations
seata
Is it safe to use flush mobile phones to speculate in stocks?