当前位置:网站首页>[※ leetcode refers to offer 48. The longest substring without repeated characters (medium)]
[※ leetcode refers to offer 48. The longest substring without repeated characters (medium)]
2022-04-23 21:21:00 【Minaldo7】
subject :
Please find the longest substring in the string that does not contain duplicate characters , Calculate the length of the longest substring .
Example 1:
Input : “abcabcbb”
Output : 3
explain : Because the longest substring without repeating characters is “abc”, So its length is 3.
Example 2:
Input : “bbbbb”
Output : 1
explain : Because the longest substring without repeating characters is “b”, So its length is 1.
Example 3:
Input : “pwwkew”
Output : 3
explain : Because the longest substring without repeating characters is “wke”, So its length is 3.
Please note that , Your answer must be Substring The length of ,“pwke” Is a subsequence , Not substring .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The problem solving process :
Classical dynamic programming solution
import java.util.Hashtable;
class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> dict = new HashMap<>();
int max = 1;
int dp[] = new int[s.length()+1]; // Always maintain the maximum length without duplicate characters
if(s.equals(""))
return 0;
dict.put(s.charAt(0),0);
dp[0] = 1;
for(int i = 1; i < s.length(); i++) {
// No repetition
if(!dict.containsKey(s.charAt(i))){
dp[i] = dp[i-1]+1;
}
// Duplicate occurs
else{
// Gets the position of the duplicate character in the atomic string
int repeat = dict.get(s.charAt(i));
if(i-repeat>dp[i-1])
dp[i] = dp[i-1]+1;
else
dp[i] = i-repeat;
}
// Update the latest element location
dict.put(s.charAt(i),i);
max = max>dp[i]?max:dp[i];
}
return max;
}
}
Execution results :
版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479334.html
边栏推荐
- Deep analysis of C language pointer (Part I)
- 危机即机遇,远程办公效率为何会提升?
- go interface
- laravel 发送邮件
- 常用60类图表使用场景、制作工具推荐
- MySQL数据库常识之储存引擎
- Sharpness difference (SD) calculation method of image reconstruction and generation domain index
- Another data analysis artifact: Polaris is really powerful
- thinkphp5+数据大屏展示效果
- 引入结构化并发,Swift 5.5 发布!
猜你喜欢
Addition, deletion, modification and query of MySQL advanced table
Question brushing plan - depth first search DFS (I)
Zhongchuang storage | how to choose a useful distributed storage cloud disk
Xiaomi mobile phone has abandoned the "Mi" brand all over the world and switched to the full name brand of "Xiaomi"
airbase 初步分析
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
41. 缺失的第一个正数
Minecraft 1.12.2模组开发(四十三) 自定义盾牌(Shield)
Arm architecture assembly instructions, registers and some problems
Win 11K in 100 days, super complete learning guide for job transfer test
随机推荐
Unit function expansion
IOT 设计与开发
Xiaomi mobile phone has abandoned the "Mi" brand all over the world and switched to the full name brand of "Xiaomi"
unity 功能扩展
Another data analysis artifact: Polaris is really powerful
Mysql database common sense storage engine
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
Recommended usage scenarios and production tools for common 60 types of charts
Pyuninstaller package exe cannot find the source code when running, function error oserror: could not get source code
What about laptop Caton? Teach you to reinstall the system with one click to "revive" the computer
Pipes and xargs
go map
Pytorch preserves different forms of pre training models
Send email to laravel
Assertionerror: invalid device ID and runtimeerror: CUDA error: invalid device ordinal
LeetCode-279-完全平方数
如何发挥测试策略的指导性作用
DeNO 1.13.2 release
Reference of custom message in ROS function pack failed
Go limit depth traversal of files in directory