方法一:用一个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. 34. CentOS-6.3安装配置Apache2.2.6

    安装说明 安装环境:CentOS-6.3安装方式:源码编译安装 软件:httpd-2.2.6.tar.gz  | pcre-8.32.tar.gz | apr-1.4.6.tar.gz | apr-u ...

  2. java msgbox

    JAVA import javax.swing.JOptionPane; JOptionPane.showMessageDialog( null,"sample dialog !" ...

  3. ubuntu16.04 dpkg强制安装 teamviewer

    dpkg遇到安装有依赖,而依赖的包有无法安装的时候,可以试试强制安装: .90154_amd64.deb 虽然报错,但是安装后还是可以使用. 如果使用: .90154_amd64.deb 提示下面错误 ...

  4. kafka 修改partition,删除topic,查询offset

    修改分区个数: ./kafka-topics./kafka/<id_of_kafka> --alter --partitions 10 --topic test_topic 上面命令将te ...

  5. find命令之时间戳使用示例

    查看当前目录以及子目录下哪些文件占用的空间最大: find  ./  -type  f  -exec du -m {} \; | sort -nr | head find  ./  -type  f ...

  6. 将Ctrl+Alt+Delete键进行屏蔽,防止误操作重启服务器

    [root@bgw-t ~]# vi  /etc/init/control-alt-delete.conf #exec /sbin/shutdown -r now "Control-Alt- ...

  7. delphi 颜色转换函数总结

    unit UColor; interface uses windows, sysutils, classes, graphics; function HexToInt(Hexa: String): L ...

  8. Linux就业技术指导(一):简历撰写及面试筹备要领

    一,开场 二,模型 三,目标选材 3.1 什么是目标选材 简单说就是确定一个候选人是否符合某一个工作岗位要求的整个流程.这是对招聘方的一个培训,应聘方如果掌握了,就知道应该怎样正确的去应聘工作. 3. ...

  9. 上传 第三方jar包 nexus

    手动上传第三方jar包到nexus的步奏: 1)按下图方式进行 2)按下图完成上传 3)点击保存完成

  10. IN_sales_order带后续P IN_ITEM_SITE带P\SP\TP DUMMY

    IN_sales_order带后续P IN_ITEM_SITE带P\SP\TP DUMMY SAP_MATERIAL_SO 处理材料订单缺少BOM,ROUTING信息