当前位置:网站首页>双指针 同向双指针、滑动窗口3、594、|27、26、80、83、82、611、187、643、674、209、438、567、424、76、30
双指针 同向双指针、滑动窗口3、594、|27、26、80、83、82、611、187、643、674、209、438、567、424、76、30
2022-04-22 14:12:00 【借点头发吧】
3. 无重复字符的最长子串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
示例 2:
输入: “bbbbb”
输出: 1
解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。
示例 3:
输入: “pwwkew”
输出: 3
解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。
请注意,你的答案必须是 子串 的长度,“pwke” 是一个子序列,不是子串。
题解:
1)滑动窗口 遍历字串,未出现的字符加入map,向右滑动,并重置max。遇到出现过的字符,将左边滑动到此位置。
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. 最长和谐子序列
和谐数组是指一个数组里元素的最大值和最小值之间的差别正好是1。现在,给定一个整数数组,你需要在所有可能的子序列中找到最长的和谐子序列的长度。
示例 1:
输入: [1,3,2,2,5,2,3,7]
输出: 5
原因: 最长的和谐数组是:[3,2,2,2,3].
题解:
1)hash映射 见哈希统计集
2)排序后双指针 类似滑动窗口
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;
}
}
版权声明
本文为[借点头发吧]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_47866171/article/details/108291054
边栏推荐
- Detailed explanation of channel implementation of cocoyaxi Library
- POM in idea Mysql5. XML file 7 coordinate red error
- Redis connection tool cannot connect to redis in docker
- An error is reported when reading the attribute value of the object through the variable storage key value in TS (TS: 7053)
- LeetCode-819 最常见的单词
- [zeekr_tech] Introduction to ros/ros 2
- 【终于等到你】微信转发语音的方法 - 语音信息转发
- C语言的三子棋,用22天总结了一份完美的SQL学习笔记
- Eight strange facts about semiconductors
- 华为云媒体査勇:华为云在视频AI转码领域的技术实践
猜你喜欢

Seven years of "one sword" and standing at the edge of the cloud, how to accelerate the digital transformation of enterprises?

【Zeekr_Tech】ROS/ROS 2介绍
![[paper notes] vision transformers for dense prediction](/img/71/aaf1509237192923ee71a5148e5502.png)
[paper notes] vision transformers for dense prediction

Golang:包管理

iMeta: 整合宏组学重新认识生命和环境科学

【终于等到你】微信转发语音的方法 - 语音信息转发

【pytorch】自己实现精简版YOLOV3【五】,实现YOLOV3损失函数(一)

LeetCode-386 字典序排数

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

I changed the bug in the morning and was informed of being laid off in the afternoon
随机推荐
A solution to the problem of buying and selling stocks by force deduction
获取数据库中数值时,数据库有值,却为空??
关于半导体的8个奇特事实
【Zeekr_Tech】ROS/ROS 2介绍
redis连接工具无法连上docker中redis
Markdown Icon
Brushless DC motor vector control (I): concept and process combing
Redis相比memcached
Advanced multithreading
双指针||有序数组去重 排序链表移除元素 26、27、83
树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
PLSQL developer file encoding format setting
The more "intelligent" the machine is, the easier it is for data taggers to be eliminated? Manfu Technology
Where do embedded software bugs come from and how do they go
Is it safe to open an account in Zhujiang futures?
2022 tea artist (intermediate) examination simulation 100 questions and simulation examination
分析电脑使用变慢的七大原因
关于QPS、TPS、并发用户数及吞吐量浅谈
【机器人学习】移动机器人里程计学习记录
TensorFlow-ValueError: ‘images‘ contains no shape