594. Longest Harmonious Subsequence
方法一:用一个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的更多相关文章
- 【Leetcode_easy】594. Longest Harmonious Subsequence
problem 594. Longest Harmonious Subsequence 最长和谐子序列 题意: 可以对数组进行排序,那么实际上只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列 ...
- 594. Longest Harmonious Subsequence - LeetCode
Question 594. Longest Harmonious Subsequence Solution 题目大意:找一个最长子序列,要求子序列中最大值和最小值的差是1. 思路:构造一个map,保存 ...
- LeetCode 594. Longest Harmonious Subsequence (最长的协调子序列)
We define a harmonious array is an array where the difference between its maximum value and its mini ...
- [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 ...
- 594. Longest Harmonious Subsequence强制差距为1的最长连续
[抄题]: We define a harmonious array is an array where the difference between its maximum value and it ...
- 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...
- [LeetCode] Longest Harmonious Subsequence 最长和谐子序列
We define a harmonious array is an array where the difference between its maximum value and its mini ...
- [Swift]LeetCode594. 最长和谐子序列 | Longest Harmonious Subsequence
We define a harmonious array is an array where the difference between its maximum value and its mini ...
- LeetCode Longest Harmonious Subsequence
原题链接在这里:https://leetcode.com/problems/longest-harmonious-subsequence/description/ 题目: We define a ha ...
随机推荐
- wzben的QQ空间
实习之后没有动过博客了,后续慢慢补.
- angular 参考文档
https://www.w3schools.com/angular/ 参考二: https://www.angular.cn/guide/reactive-forms
- VBA 浏览文件夹
Private Function SelectFolder() As String With Application.FileDialog(msoFileDialogFolderPick ...
- Ping ip能ping通,ping计算机名ping不通,网络共享不能访问
名称 协议 端口 NetBIOS Name Service UDP 137 NetBIOS Datagram Service UDP 138 NetBIOS Session Service TCP 1 ...
- 10 python os&sys 模块
1.os模块 os模块提供了很多允许你的程序与操作系统直接交互的功能 os模块的主要功能:处理文件和目录,系统相关,执行命令,管理进程 检验给出的路径是否是一个文件:os.path.isfile() ...
- 使用JavaScript的XMLHttpRequest发送POST、GET请求以及接收返回值
使用XMLHttpRequest对象分为4部完成: 1.创建XMLHttpRequest组建 2.设置回调函数 3.初始化XMLHttpRequest组建 4.发送请求 实例代码: [javascri ...
- 管道限流利器pv
pv 是什么 可不是 page view,是pipe viewer,管道偷窥器的缩写.这个东西的源站点在google code上,需要的话可以访问pv 的官网 . 这个东西的官方手册页(man pv或 ...
- nginx配置【转】
转自:http://www.ha97.com/5194.html #定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_proc ...
- (转)游戏引擎中三大及时光照渲染方法介绍(以unity3d为例)
重要:在目前市面上常见的游戏引擎中,主要采用以下三种灯光实现方式: 顶点照明渲染路径细节 Vertex Lit Rendering Path Details 正向渲染路径细节 Forward Rend ...
- hive 显示分区
显示某一张表的分区值 show partitions table_name;