当前位置:网站首页>力扣每日一题-第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;
}
};边栏推荐
- VLAN experiment
- Boost the digital economy and face the future office | the launch of the new version of spreadjsv15.0 is about to begin
- 5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin ((tdmpp) H2) /2-nitro-5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin copper (no2tdmpp) Cu) supplied by Qiyue
- 想请教一下,究竟有哪些劵商推荐?在线开户是安全么?
- Stack -- 739 Daily temperature
- 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?
- Client and server working modes of JVM
- Design of leetcode simple problem goal parser
- Servlet version conflict causes page 404
- Creation of Arduino uno development environment
猜你喜欢

Difference between URI and URL

Servlet version conflict causes page 404

Devops development, operation and maintenance Basics: using Jenkins to automatically build projects and notify by email

Test Development - ten years of sharpening one sword (VII) interface test tool postman

Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately

ES6 Modularization: export /import

The generation of leetcode simple questions each character is an odd number of strings

MySQL add / delete / modify query SQL statement exercise yyds dry goods inventory

Top ten Devops best practices worthy of attention in 2022

Browser local storage
随机推荐
Servlet version conflict causes page 404
About: deleting unwanted event log lists
ICLR is going to have a big discussion on the deep generation model. Max welling and the winner of the AAAI million dollar award are here. Bengio is one of the organizers
2022.02.14 - 239. A single element in an ordered array
證券開戶安全麼,有沒有什麼危險呢
Why can't the article be posted?
Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
Principle of screen printing adjustment of EDA (cadence and AD) software
The simple problem of leetcode is to divide an array into three parts equal to sum
[C language series] - branch and loop statements
What is the "danksharding" of V God Kop on Valentine's day?
Use of sed in shell script
[C language series] - initial C language (4)
Slot
Week 10 - task 0- execution process instance resolution of constructors and destructors
Benign competition will promote each other
Meta metauniverse female safety problems occur frequently. How to solve the relevant problems in the metauniverse?
Ctrip launched the "3+2" office mode. Are you sour?
Why Houdini made the pyside2 plug-in crash
How to insert pseudo code into word documents simply and quickly?