【Leetcode_easy】594. Longest Harmonious Subsequence
problem
594. Longest Harmonious Subsequence
最长和谐子序列
题意:
可以对数组进行排序,那么实际上只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列的长度了.
solution1: 使用hashmap
用 HashMap 来做,先遍历一遍,建立每个数字跟其出现次数之间的映射,然后再遍历每个数字的时候,只需在 HashMap 中查找该数字加1是否存在,存在就更新结果 res.
class Solution {
public:
int findLHS(vector<int>& nums) {
int res = ;
unordered_map<int, int> mymap;
for (auto num : nums) mymap[num]++;
for(auto a : mymap)
{
if(mymap.count(a.first+))
{
res = max(res, mymap[a.first]+mymap[a.first+]);
}
}
return res;
}
};
参考
1. Leetcode_easy_594. Longest Harmonious Subsequence;
2. Grandyang;
完
【Leetcode_easy】594. Longest Harmonious Subsequence的更多相关文章
- 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- 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】300.Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 【Lintcode】077.Longest Common Subsequence
题目: Given two strings, find the longest common subsequence (LCS). Your code should return the length ...
- 【Lintcode】076.Longest Increasing Subsequence
题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...
随机推荐
- dede织梦调用顶级二级栏目及下三级栏目方法(数据库实现)
上次有说道能调用织梦的二级栏目今天来说道说道调用三级,乃至无限极 ①:通过dede调用二级栏目大家都会调用,但要调用三级栏目,就有点麻烦了,如下样式的三级栏目dede如何调用呢?如下: ------- ...
- 02_View
1.View 1.基于类的视图 Class-based Views REST framework提供APIView是Django的View的子类 发送到View的Request请求:是REST fra ...
- document基本操作 动态脚本-动态样式-创建表格
var html = document.documentElement; var body = document.body; window.onload = function() { //docume ...
- bootstrap富文本编辑
先把设定富文本框架 <div class="form-group"> <label class="col-sm-2 control-label" ...
- webpack 性能优化小结
背景 如今前端工程化的概念早已经深入人心,选择一款合适的编译和资源管理工具已经成为了所有前端工程中的标配,而在诸多的构建工具中,webpack以其丰富的功能和灵活的配置而深受业内吹捧,逐步取代了gru ...
- decodeURI 与 decodeURIComponent 区别
1. 关于URL.encodeURI 及 encodeURIComponent: URI: Uniform Resource Identifiers,通用资源标识符 Global 对象的 encode ...
- mac 启动mysql
sudo /usr/local/mysql/support-files/mysql.server stop sudo /usr/local/mysql/support-files/mysql.serv ...
- mac 登陆phpmyadmin 提示 mysqli_real_connect(): (HY000/2002): No such file or directory
我们将下载的phpmyadmin 放在apache目录中,进入phpmyadmin目录, 首先将这个目录中的配置文件改名 sudo mv config.sample.inc.php config.in ...
- nginx 动态黑名单
原理: 根据nginx 访问日志记录发现可疑的或者不正常的访问记录记录然后自动添加到nginx的黑名单 起到阻止的作用 可以作为防范少量的ddos攻击 1.首先要格式化nginx的日志(相关内容可以 ...
- C++生成DM数据点导入DM
在c++编写正弦曲线点的代码,源代码如下: //想要使用内置的π,此句必不可少! #define _USE_MATH_DEFINES #include<iostream> #include ...