当前位置:网站首页>[※ 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
边栏推荐
- MySQL advanced common functions
- Explore ASP Net core read request The correct way of body
- 41. 缺失的第一个正数
- 笔记本电脑卡顿怎么办?教你一键重装系统让电脑“复活”
- presto on spark 支持3.1.3记录
- Reference of custom message in ROS function pack failed
- MySQL进阶之数据的增删改查(DML)
- 韩国或将禁止苹果和谷歌向开发者抽佣 创全球首例
- Google tries to use rust in Chrome
- Introduce structured concurrency and release swift 5.5!
猜你喜欢
C, print the source program of beautiful bell triangle
Recommended usage scenarios and production tools for common 60 types of charts
airbase 初步分析
[leetcode refers to offer 32 - III. print binary tree III from top to bottom (medium)]
What if Jenkins forgot his password
2. Finishing huazi Mianjing -- 2
Pipes and xargs
Flomo software recommendation
Prim、Kruskal
随机推荐
一些接地气的话儿
MySQL advanced common functions
Deep analysis of C language pointer (Part I)
Addition, deletion, modification and query of MySQL advanced table
MySQL进阶之常用函数
2. Finishing huazi Mianjing -- 2
unity 功能扩展
一文解决浏览器跨域问题
What about laptop Caton? Teach you to reinstall the system with one click to "revive" the computer
PHP的Laravel与Composer部署项目时常见问题
Thinkphp5 + data large screen display effect
setInterval、setTimeout、requestAnimationFrame
Some grounded words
ubutnu20安裝CenterNet
小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌
Problem brushing plan -- dynamic programming (IV)
Prim、Kruskal
Recommended usage scenarios and production tools for common 60 types of charts
1. Finishing huazi Mianjing -- 1