当前位置:网站首页>力扣每日一题-第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;
}
};边栏推荐
- Internet enterprises need CRM software to help
- Regular expressions for shell script values
- 2022-01 Microsoft vulnerability notification
- 51 lines of code, self-made TX to MySQL software!
- Week 10 - task 0- execution process instance resolution of constructors and destructors
- Est - ce que l'ouverture d'un compte de titres est sécurisée? Y a - t - il un danger?
- Leetcode theme [array] -217- there are duplicate elements
- Servlet version conflict causes page 404
- Segment in Lucene
- Fault: NetBt log for id4321
猜你喜欢

β- Tetraphenyl nickel porphyrin with all chlorine substitution| β- Thiocyano tetraphenyl porphyrin copper| β- Dihydroxy tetraphenyl porphyrin 𞓜 2-nitroporphyrin | supplied by Qiyue

Agile invincible event

QT (x): innosetup for software packaging

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?

2,5-di (3,4-dicarboxyphenoxy) - 4 '- phenylethynylbiphenyldianhydride (pephqda) / Qiyue custom supply porphyrin modified amphiphilic block copolymer peg113-pcl46-porphyrin

Hyperledger Fabric 2. X custom smart contract

What has urbanization brought to our mental health and behavior?

Manual (functional) test 1

MySQL learning notes

Why are keys unordered in golang map
随机推荐
Why are keys unordered in golang map
Convert data frame with date column to timeseries
Quickly write MVVM code using source generators
Difference between parametric continuity and geometric continuity
Analysis comp122 the Caesar cipher
String and variable are spliced into variable name
Jenkins operation Chapter 5 trigger, configuration webhook, Jenkins parameterized construction
2022-01 Microsoft vulnerability notification
Difference between static and final
Mongodb paging method
QT (x): control operation
What are the uses of wireless pressure collectors?
Establishing the development environment of esp8266
Call the computer calculator and use it to convert several base numbers
[high concurrency] deeply analyze the callable interface
Leetcode notes on question brushing (XIV) -- related topics of binary tree attributes in binary tree chapter
[chromium] win10 vs2019 environment chromium configuration and compilation.
Is there any difference between a=a+b and a+=b?
Review of MySQL knowledge points
What is MES? What does it do?