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的更多相关文章

  1. 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...

  2. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  3. 594. Longest Harmonious Subsequence - LeetCode

    Question 594. Longest Harmonious Subsequence Solution 题目大意:找一个最长子序列,要求子序列中最大值和最小值的差是1. 思路:构造一个map,保存 ...

  4. LeetCode 594. Longest Harmonious Subsequence (最长的协调子序列)

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

  5. [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 ...

  6. 594. Longest Harmonious Subsequence强制差距为1的最长连续

    [抄题]: We define a harmonious array is an array where the difference between its maximum value and it ...

  7. 【leetcode】300.Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  8. 【Lintcode】077.Longest Common Subsequence

    题目: Given two strings, find the longest common subsequence (LCS). Your code should return the length ...

  9. 【Lintcode】076.Longest Increasing Subsequence

    题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...

随机推荐

  1. 使用itchat进行自动微信聊天

    import itchat def we_chat(message): #enableCmdQR=2用于linux中显示二维码,hotReload=True退出程序后暂存登录状态 itchat.aut ...

  2. js手机点击图片放大

    点击每个图片获取到对应的img的url链接,再把链接给一个空img以此来实现 最终效果:

  3. BZOJ1209 最佳包裹 (三维凸包 增量法)

    题意 求三维凸包的表面积. N≤100N\le100N≤100 题解 暴力往当前的凸包里加点.O(n2)O(n^2)O(n2).题解详见大佬博客 扰动函数shakeshakeshake是为了避免四点共 ...

  4. Codeforces Round #464 (Div. 2) D题【最小生成树】

    Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her b ...

  5. sql server 将某一列的值拼成一个字符串 赋值到一个字段内

    DECLARE @refCodeitems VARCHAR(800),   SELECT @refCodeitems=ISNULL(@refCodeitems,'')+refCodeitem +'/' ...

  6. HEML与Css的基本理解

    什么是 HTML? HTML 就像造房子一样,一栋房子有多个组成部分,html类似于房子的户型,它设计了房子的整体架构.分区.布局,而且还定义了每个区块的功能作用.html技术为后续入住的数据事先搭建 ...

  7. 洛谷 P1076 寻宝 题解

    今天又TM考试了...... 这是T1,然后我模拟20分滚粗. Analysis 在每层的时候用编号%这层可以上楼的房间个数就行了. #include<iostream> #include ...

  8. 2.设计模式-Abstract Factory 抽象工厂模式

    大神勿喷,不对的地方请指出来,学笔记而已. 解决的问题:应对多系列对象构建的变化或多系列(例如:崎岖的山路和平坦的马路属于一个系列) 不断的变化的创建. 使用场景:对象不变(比如有3个对象 " ...

  9. 洛谷P1419寻找段落

    题目 单调队列+前缀和 #include <bits/stdc++.h> #define N 101001 using namespace std; int n, s, t; int da ...

  10. redis简单消息队列

    <?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); $redis->flushall(); $redis-& ...