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

  这道题给我们一个数字array, 让我们找出最长协调的子序列的长度。

  首先把每一个数字 和它出现次数 存入 map。

  然后遍历map 的 keySet,对于每一个key,检查 key + 1 是否也存在,存在的话,就把 key 和 key + 1 的value 相加,保留一个最大的 max 就是最长的长度。  

Java Solution:

Runtime beats 69.23%

完成日期:06/07/2017

关键词:HashMap

关键点:遍历key,把key 和 key + 1 的value 相加

 class Solution
{
public int findLHS(int[] nums)
{
HashMap<Integer, Integer> map = new HashMap<>();
int res = 0; for(int num: nums)
map.put(num, map.getOrDefault(num, 0) + 1); // for each key, check its key + 1
for(int key: map.keySet())
{
if(map.containsKey(key + 1))
res = Math.max(res, map.get(key) + map.get(key + 1)); } return res;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 594. Longest Harmonious Subsequence (最长的协调子序列)的更多相关文章

  1. [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  2. 【Leetcode_easy】594. Longest Harmonious Subsequence

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

  3. 594. Longest Harmonious Subsequence - LeetCode

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

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

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

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

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

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

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

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

  8. [LeetCode] Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  9. [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

随机推荐

  1. panda库------对数据进行操作---合并,转换,拼接

    >>> frame2 addr age name 0 beijing 12 zhang 1 shanghai 24 li 2 hangzhou 24 cao >>> ...

  2. Class类与Java反射

    1反射机制是什么 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为jav ...

  3. [01] Pattern类和Matcher类

    在Java中,有个java.util.regex包,这是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. 它主要有两个类: Pattern   一个正则表达式经编译后的表现模式,可以理解为 ...

  4. scoke摘要

      登录|注册     关闭 永不磨灭的意志 /* ----------------500G的电影拷到了U盘上,U盘的重量会不会增加?----------------------*/       目录 ...

  5. windows 结束进程的详细过程

    windows上如何结束进程的详细过程,下面附详细,图文说明 在cmd下,输入  netstat   -ano|findstr  8080      //说明:查看占用8080端口的进程 在cmd下, ...

  6. Delphi中paramstr的用法

    原型                function   paramstr(i:index):string        对于任何application paramstr(0)都默认代表的是应用程序的 ...

  7. [js高手之路] html5 canvas系列教程 - 线形渐变,径向渐变与阴影设置

    接着上文[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)继续. 一.线形渐变 线形渐变指的是一条直线上发生的渐变. 用法: var linear ...

  8. 【Kafka】操作命令

    生产者 ./kafka-console-producer.sh --broker-list --topic norm 消费者 ./kafka-console-consumer.sh --zookeep ...

  9. SiganlR 系列之概述

    简介 SignalR 是微软的 http 长连接(以下简称长连接)框架,它的出现为我们提供了一套行之有效的实时通信的解决方案. 背景 在http 1.0 时代,preRequest 都会建立新的tcp ...

  10. P1013

    问题 D: P1013 时间限制: 1 Sec  内存限制: 128 MB提交: 33  解决: 21[提交][状态][讨论版] 题目描述 " 找啊找啊找GF,找到一个好GF,吃顿饭啊拉拉手 ...