方法一:用一个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. DevExpress ImageComboBoxEdit增加

    Combo_订单类型.Properties.Items.Clear() Select Case Combo_客户名称.EditValue Case "ABC" Combo_订单类型 ...

  2. adb的一些常用的命令

    如果在dos界面想要直接用adb的话,需要将anroidsdk安装目录下的tools和platform-tools以及加入到环境变量path中. 查看当前的设备(包括真机和模拟器):adb devic ...

  3. Cache Lucene IndexReader with Apache Commons Pool

    IndexReaderFactory.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2 ...

  4. python面试题(转)

    下面的代码输出什么? list = ['a', 'b', 'c', 'd', 'e'] print list[10:] 上面的代码输出[],并且不会导致IndexError错误 跟你想的一样,当取列表 ...

  5. 使用Fiddler发送POST请求

    使用Fiddler发送POST请求 在测试过程中,有时会遇到需要修改请求中带的参数,去验证权限的安全问题,但是一些post请求,我们在浏览器中不能直接修改他的参数,然后去提交验证. 而fiddler可 ...

  6. IEdevelopToolbar ie浏览器的css代码调试工具

    使用IEdevelopToolbar的“选择元素”工具(ctrl+b),选取你要内容的地方下方的DIV,我们就可以找到几个关键字

  7. 利用 AWK 的数值计算功能提升工作效率(转载)

    Awk 是一种优秀的文本样式扫描和处理工具.转文侧重介绍了 awk 在数值计算方面的运用,并通过几个实际工作中的例子,阐述了如何利用 awk 的计算功能来提高我们的工作效率.转文源自IBM Bluem ...

  8. 求2的n次方对1e9+7的模,n大约为10的100000次方(费马小定理)

    昨天做了一个题,简化题意后就是求2的n次方对1e9+7的模,其中1<=n<=10100000.这个就算用快速幂加大数也会超时,查了之后才知道这类题是对费马小定理的考察. 费马小定理:假如p ...

  9. ios8 UITableView section不显示

    ios8 如果UITableView只设置viewForHeaderInSection,则可能section不能显示,iOS7及以下版本显示正常. 解决方案: 设置heightForHeaderInS ...

  10. [leetcode]242. Valid Anagram验证变位词

    Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...