原题链接在这里:https://leetcode.com/problems/longest-harmonious-subsequence/description/

题目:

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

Now, given an integer array, you need to find the length of its longest harmonious subsequence among all its possible subsequences.

Example 1:

Input: [1,3,2,2,5,2,3,7]
Output: 5
Explanation: The longest harmonious subsequence is [3,2,2,2,3].

Note: The length of the input array will not exceed 20,000.

题解:

在HashMap<Integer, Integer> hm中计数nums中每个element出现次数.

再看hm的key加一是否也在hm中能找到, 若能计算个数sum更新res.

Time Complexity: O(n). Space: O(n).

AC Java:

 class Solution {
public int findLHS(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
} int res = 0;
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int n : nums){
hm.put(n, hm.getOrDefault(n, 0)+1);
} for(int i : hm.keySet()){
if(hm.containsKey(i+1)){
res = Math.max(res, hm.get(i)+hm.get(i+1));
}
}
return res;
}
}

LeetCode Longest Harmonious Subsequence的更多相关文章

  1. [LeetCode] Longest Harmonious Subsequence 最长和谐子序列

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

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

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

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

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

  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. C#LeetCode刷题之#594-最长和谐子序列​​​​​​​​​​​​​​(Longest Harmonious Subsequence)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3800 访问. 和谐数组是指一个数组里元素的最大值和最小值之间的差 ...

  6. 594. Longest Harmonious Subsequence - LeetCode

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

  7. [Swift]LeetCode594. 最长和谐子序列 | Longest Harmonious Subsequence

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

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

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

  9. 【Leetcode_easy】594. Longest Harmonious Subsequence

    problem 594. Longest Harmonious Subsequence 最长和谐子序列 题意: 可以对数组进行排序,那么实际上只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列 ...

随机推荐

  1. c# 泛型(Generic)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 泛型 { ...

  2. python 课堂笔记-if语句

    # Author:zyl _username = 'zyl' _password = 'zyl123' username = input("username:") password ...

  3. python函数模拟mysql增删改查功能

    import os list1 = ['staff_id', 'name', 'age', 'phone', 'dept', 'enroll_date'] def staff_info(): #获取员 ...

  4. axios拦截器/http

    Interceptors //处理请求或响应之前拦截请求或响应. //添加一个请求拦截器 axios.interceptors.request.use(function (config) { //在请 ...

  5. INSPIRED启示录 读书笔记 - 第14章 产品评审团

    制定更及时.更可靠的产品决策 制定决策通常是既耗时又费力的,产品公司需要一套机制让决策者和相关人员及时作出明智的产品决策.成立产品评审团是最好的解决途径 组织产品评审团的难点在于既要为高管制定产品决策 ...

  6. PHP获取MySQL执行sql语句的查询时间

    //计时开始 runtime(); //执行查询 mysql_query($sql); //计时结束. echo runtime(1); //计时函数 function runtime($mode=0 ...

  7. MYSQL数据库字段命名及设计规范

    1.设计原则 1) 标准化和规范化数据的标准化有助于消除数据库中的数据冗余.标准化有好几种形式,但 Third Normal Form(3NF)通常被认为在性能.扩展性和数据完整性方面达到了最好平衡. ...

  8. 发现程序bug思路

    大家有没有遇到过项目,程序出现个bug,但花了好久(真的是a long long time啊)才发现引发这个问题的原因,心想原来就这个原因导致的啊,要是早想到就好了! 其实我们确实的是方法,希望我的抛 ...

  9. QT 使用QSettings读写ini配置文件

    利用Qsettings包一个类 RWIniFile, writeIni方法写文件, readIni方法读文件 rwinifile.h #ifndef RWINIFILE_H #define RWINI ...

  10. Qt qobject_cast用法 向下转型

    函数原型: T qobject_cast ( QObject * object ) 本方法返回object向下的转型T,如果转型不成功则返回0,如果传入的object本身就是0则返回0. 在使用时有两 ...