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 ...
随机推荐
- time 时间内置模块3种形态的转化
import time print(time.time()) #获得时间戳 1526998642.877814 print(time.sleep(2)) #停止2秒 print(time.gmti ...
- gradle问题 cordova
cordova升级7.0后,运行 > ionic build android 或者 cordova build android 报出错误 Error: Could not find a ...
- find命令之时间戳使用示例
查看当前目录以及子目录下哪些文件占用的空间最大: find ./ -type f -exec du -m {} \; | sort -nr | head find ./ -type f ...
- TEXT 15 A text a day...
TEXT 15 A text a day... Mar 24th 2006 From The Economist print edition The medical uses of mobile ph ...
- 数据文件导入mysql时出现中文乱码问题
http://www.ynpxrz.com/n773257c2024.aspx 从shell进入mysql, mysql> show variables like ‘%char%'; +---- ...
- Spring Retry
最近组内准备将项目中原有的重试功能抽取出来重构为一个重试平台,由于对重试的功能要求比较高,采用了不少中间件和框架(jimdb,jproxy, Elastic-Job ,JMQ,Hbase, Disru ...
- Python repr() 函数
Python repr() 函数 Python 内置函数 描述 repr() 函数将对象转化为供解释器读取的形式. 语法 以下是 repr() 方法的语法: repr(object) 参数 obje ...
- GridView自定义分页样式(上一页,下一页,到第几页)
今天要为网站做一个文章列表,发现GridView的分页样式很难看,于是结合网上的例子,自己做了一个.不是很美观,不过还是很实用的,先看下效果吧,如图(1). 图(1)GridView分页效果 自定义G ...
- C++ define与const
C++中不但可以用define定义常量还可以用const定义常量,它们的区别如下: 用#define MAX 255定义的常量是没有类型的,所给出的是一个立即数,编译器只是把所定义的常量值与所定义的常 ...
- [udemy]WebDevelopment_How the Internet Works
Browsing the web Enter google.com, who is this google.com This question gets asked all the way down ...