方法一:用一个map来辅助,代码简单,思路清晰

 static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int findLHS(vector<int>& nums)
{
unordered_map<int,int> imap;
for(int i:nums)
imap[i]++;
int count=;
for(auto p:imap)
{
if(imap.find(p.first+)!=imap.end())
{
count=max(count,p.second+imap[p.first+]);
}
}
return count;
}
};

方法二:排序,然后扫描数组,一遍扫描一边统计

 static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int findLHS(vector<int>& nums)
{
int sz=nums.size();
if(sz<)
return ;
sort(nums.begin(),nums.end());
int temp=,pre=,longest=;
for(int i=;i<sz;i++)
{
if(nums[i]==nums[i-])
temp++;
else if(nums[i]==nums[i-]+)
{
longest=max(longest,pre?temp+pre:);
pre=temp;
temp=;
}
else
{
longest=max(longest,pre?temp+pre:);
temp=;
pre=;
}
}
longest=max(longest,pre?temp+pre:);
return longest;
}
};

第二种方法比较好理解,不多说。方法一62ms,方法二42ms。

594. Longest Harmonious Subsequence的更多相关文章

  1. 【Leetcode_easy】594. Longest Harmonious Subsequence

    problem 594. Longest Harmonious Subsequence 最长和谐子序列 题意: 可以对数组进行排序,那么实际上只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列 ...

  2. 594. Longest Harmonious Subsequence - LeetCode

    Question 594. Longest Harmonious Subsequence Solution 题目大意:找一个最长子序列,要求子序列中最大值和最小值的差是1. 思路:构造一个map,保存 ...

  3. LeetCode 594. Longest Harmonious Subsequence (最长的协调子序列)

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

  4. [LeetCode&Python] Problem 594. Longest Harmonious Subsequence

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

  5. 594. Longest Harmonious Subsequence强制差距为1的最长连续

    [抄题]: We define a harmonious array is an array where the difference between its maximum value and it ...

  6. 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...

  7. [LeetCode] Longest Harmonious Subsequence 最长和谐子序列

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

  8. [Swift]LeetCode594. 最长和谐子序列 | Longest Harmonious Subsequence

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

  9. LeetCode Longest Harmonious Subsequence

    原题链接在这里:https://leetcode.com/problems/longest-harmonious-subsequence/description/ 题目: We define a ha ...

随机推荐

  1. robot framework添加库注意事项

    添加库 假设你的项目结构是这样: 项目 ..myLib(库目录) ..目录1 ..测试用例套件1 此时你需要在“测试用例套件1”中用相对路径添加库myLib,你应该填:../myLib/ 特别注意后面 ...

  2. Yii常用变量

    调用YII框架中 jquery:Yii::app()->clientScript->registerCoreScript('jquery'); 调用YII框架中 jquery:Yii::a ...

  3. 使用VB.Net Express版本创建服务

    Services Part 1:>  Creating Services Visual Basic Express is a great, free tool from Microsoft.   ...

  4. ActiveMQ无法启动

    解决办法:activemq无法启动,端口被占用 用netstat -an无法查出61616被哪个进程占用(实践证明,netstat -ano|findstr '61616'什么也没有找到) 经过排查和 ...

  5. 15 python re 模块

    1.基础知识 正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎 正则表达式的大致匹配流程:依次拿出表达式和文本中的字符比较, 如果每一个字符都能匹配,则匹配成功:一旦有匹 ...

  6. CryptographyUtil盐加密

    import org.apache.shiro.crypto.hash.Md5Hash; /** * 加密工具 * @author Administrator * */ public class Cr ...

  7. UGUI RectTransform

    RectTransform解析 当 Anchor 在同一点时,显示的是物体的座标与大小Pos X.Pos Y.Width.Height ,当 Anchor 不在同一点时(此时会形成矩形),显示的会是 ...

  8. Why ExerciseAlone May Not Be the Key to Weight Loss

    加强运动也许并不能减肥Why ExerciseAlone May Not Be the Key to Weight LossIf you give a mouse a running wheel, i ...

  9. Haskell语言学习笔记(41)Parsec(1)

    Parsec Parsec是一个词法及语法分析器. 匹配字符与字符串 Prelude Text.Parsec> parseTest anyChar "a" 'a' Prelu ...

  10. 播放一个wav文件

    use mmsystem;SndPlaySound('hello.wav',SND_FILENAME or SND_SYNC) ///////////////////////////////////u ...