当前位置:网站首页>Double pointer in the same direction, double pointer, sliding window 3, 594, |27, 26, 80, 83, 82, 611, 187, 643, 674, 209, 438, 567, 424, 76, 30
Double pointer in the same direction, double pointer, sliding window 3, 594, |27, 26, 80, 83, 82, 611, 187, 643, 674, 209, 438, 567, 424, 76, 30
2022-04-22 14:13:00 【Borrow some hair】
3. Longest substring without repeating characters
Given a string , Please find out that there are no duplicate characters in it Longest substrings The length of .
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 .
Answer key :
1) The sliding window Traversal string , A character that does not appear map, Slide to the right , And reset max. Characters encountered , Slide the left side to this position .
sloution:
class Solution {
public int lengthOfLongestSubstring(String s) {
if (s.length() == 0)
return 0;
HashMap<Character, Integer> map = new HashMap<>();
int max = 0;
for (int i = 0, j = 0; i < s.length(); ++i) {
if (map.containsKey(s.charAt(i))) {
j = Math.max(j, map.get(s.charAt(i)) + 1);
}
map.put(s.charAt(i), i);
max = Math.max(max, i - j + 1);
}
return max;
}
}
594. The longest harmonious subsequence
A harmonious array means that the difference between the maximum and minimum values of elements in an array is just 1. Now? , Given an array of integers , You need to find the length of the longest harmonious subsequence among all possible subsequences .
Example 1:
Input : [1,3,2,2,5,2,3,7]
Output : 5
reason : The longest harmonious array is :[3,2,2,2,3].
Answer key :
1)hash mapping See hash statistics set
2) Double pointer after sorting Similar to sliding window
sloution:
class Solution {
public int findLHS(int[] nums) {
Arrays.sort(nums);
int begin = 0,res = 0;
for(int end = 0;end < nums.length;end++){
while(nums[end] - nums[begin] > 1)
begin++;
if(nums[end] - nums[begin] == 1)
res = Math.max(res,end - begin + 1);
}
return res;
}
}
版权声明
本文为[Borrow some hair]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221411324292.html
边栏推荐
- Leetcode-386 dictionary order
- [pytorch] implement the simplified version of yoov3 [v] and realize the yoov3 loss function (I)
- Error reported by uniapp to wechat developer tool - [wrong content of app.json file] JSON: the sitemap.json file corresponding to ["sitemaplocation"] was not found
- 图 钥匙和房间
- Markdown Icon
- translucentTB汉化界面 - 菜单汉化 - 怎么使用 - win11任务栏透明效果
- Detailed explanation of channel implementation of cocoyaxi Library
- 【终于等到你】微信转发语音的方法 - 语音信息转发
- How to use openfeign to call the third-party interface
- 獲取數據庫中數值時,數據庫有值,卻為空??
猜你喜欢

Cannot read property 'forceupdate' of undefined - wechat developer tool reports an error

Solve command line is too long Shorten command line for........ error

Exercises and answers for the examination of main principals of hazardous chemical business units in 2022

CRM系统改善客户体验的方法

Independent station operation | 6 Facebook promotion tips, do you know?

Operation of 2022 chemical automation control instrument examination practice question simulation examination platform

Solution to the blank page of small and medium-sized programs running from uniapp to wechat developer tool

2022 tea artist (intermediate) examination simulation 100 questions and simulation examination

Multithreading primary

redis连接工具无法连上docker中redis
随机推荐
TensorFlow-ValueError: ‘images‘ contains no shape
Analyze the seven reasons for the slow use of computers
Multithreading primary
Activity preview | on April 23, a number of wonderful openmldb sharing came, which lived up to the good time of the weekend!
Fastdfs installation and configuration
LeetCode——字符的最短距离
Qiniu school asks you to download Dragonfly gold to open an account before you can continue to study? Can I download it? Is it safe?
字符串的反转练习 344、541、557、151
985 official announcement: international ranking is no longer a construction goal!
Operation of 2022 chemical automation control instrument examination practice question simulation examination platform
PLSQL developer file encoding format setting
A solution to the problem of buying and selling stocks by force deduction
redis连接工具无法连上docker中redis
[zeekr_tech] Introduction to ros/ros 2
独立站运营 | 6个Facebook推广小技巧,你都知道吗?
uniapp转微信小程序报错Cannot read property ‘forceUpdate‘ of undefined - 微信开发者工具报错
Timer--
translucentTB汉化界面 - 菜单汉化 - 怎么使用 - win11任务栏透明效果
Leetcode-3 longest substring without duplicate characters
Redis batch delete data (wildcard)