当前位置:网站首页>力扣每日一题-第30天-594.最长和谐子序列
力扣每日一题-第30天-594.最长和谐子序列
2022-06-29 06:32:00 【重邮研究森】
2022.6.28今天你刷题了吗?
题目:
和谐数组是指一个数组里元素的最大值和最小值之间的差别 正好是 1 。
现在,给你一个整数数组 nums ,请你在所有可能的子序列中找到最长的和谐子序列的长度。
数组的子序列是一个由数组派生出来的序列,它可以通过删除一些元素或不删除元素、且不改变其余元素的顺序而得到。
分析:
给你一个数组,找到里面可以构成,|x-y|=1条件时,x和y加起来的元素数目最多。
思路:我们把每个数存进哈希表。然后判断哈希表中键值是否存在相差为1的,如果有,则把两个键值对应的键值对求出来,然后找到最大的和。
解析:
1.哈希表
class Solution {
public:
int findLHS(vector<int>& nums) {
unordered_map<int, int> cnt;
int res = 0;
for (int num : nums) {
cnt[num]++;
}
for (auto it : cnt)
{
if (cnt.count(it.first + 1))
{
//it.second当前的second
//cnt[it.first + 1]下一个的second
res = max(res, it.second + cnt[it.first + 1]);
}
}
return res;
}
};2.双指针
我们可以利用两个指针,一个指向小的数,一个指向大的数,如果两个指针之间相差不为1,则把前,后指针不断后移,当找到了满足差为1时,则记录此时的下标差,然后后指针继续后移,判断下一个数是否满足。
class Solution {
public:
int findLHS(vector<int>& nums) {
sort(nums.begin(), nums.end());
int begin = 0;
int end = 0;
int res = 0;
for (auto end = 0; end < nums.size(); end++)
{
while (nums[end] - nums[begin] > 1)
{
begin++;
}
if (nums[end] - nums[begin] == 1)
{
res = max(res, end - begin + 1);
}
}
return res;
}
};边栏推荐
- Client and server working modes of JVM
- Manual (functional) test 1
- Pytest (7) -yield and termination function
- 2,5-di (3,4-dicarboxyphenoxy) - 4 '- phenylethynylbiphenyldianhydride (pephqda) / Qiyue custom supply porphyrin modified amphiphilic block copolymer peg113-pcl46-porphyrin
- 2-nitro-5,10,15,20-tetra (4-methylphenyl) porphyrin copper (no2tmpp) Cu) /2-nitro-5,10,15,20-tetra (4-methylphenyl) porphyrin (no2tmpp) H2) Qiyue porphyrin supply
- Introduction to Ceres Quartet
- Sum of digits under k-ary representation of leetcode simple problem
- Are there too many programmers in China at present?
- How to use regex in file find
- Where is the Gcov symbol- Where are the gcov symbols?
猜你喜欢

Manual (functional) test 1
![[chromium] win10 vs2019 environment chromium configuration and compilation.](/img/20/428e6b22ed6955a732dd14d5ff0e3d.jpg)
[chromium] win10 vs2019 environment chromium configuration and compilation.

Plugin

Hustoj SPJ example

Fault: NetBt log for id4321

Introduction to Ceres Quartet

QT writing map comprehensive application 58 compatible with multi browser kernel

Boost the digital economy and face the future office | the launch of the new version of spreadjsv15.0 is about to begin

Why can't the article be posted?

MySQL add / delete / modify query SQL statement exercise yyds dry goods inventory
随机推荐
Introduction to Ceres Quartet
String and variable are spliced into variable name
Venn diagram proportional and color shading with semi transparency
MySQL add / delete / modify query SQL statement exercise yyds dry goods inventory
QT (x): packaging and deployment
2022.02.15 - 240. Lucky number in matrix
Benign competition will promote each other
2022.02.14
Can I cast int to a variable of type byte? What happens if the value is larger than the range of byte type?
Part 63 - interpreter and compiler adaptation (II)
Use of sed in shell script
Can use the mouse, will reinstall the computer system tutorial sharing
Fresnel diffraction with rectangular aperture based on MATLAB
Will the order of where conditions in MySQL affect the union index? Will where 1 =1 affect the use of the index? Does where 1 =1 affect the use of indexes?
Teach you how to develop your own NPM package (publish to the NPM official website)
想请教一下,究竟有哪些劵商推荐?在线开户是安全么?
What are the uses of final?
Mongodb basic knowledge summary
Installing modules in pycharm
Design and practice of kubernetes cluster and application monitoring scheme