LeetCode 594. Longest Harmonious Subsequence (最长的协调子序列)
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 (最长的协调子序列)的更多相关文章
- [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- 【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] Longest Harmonious Subsequence 最长和谐子序列
We define a harmonious array is an array where the difference between its maximum value and its mini ...
- 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...
- [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] Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
随机推荐
- java:数组操作工具类 java.util.Arrays包 主要方法详解
Arrays类位于Java.util包下,是一个对数组操作的工具类,现将Arrays类中的方法做一个总结(JDK版本:1.6.0_34).Arrays类中的方法可以分为八类: sort(对数组排序) ...
- Maven常见错误以及解决方案【转载】
常见的Maven错误 当遇到401错误的时候,看看自己当前使用的Maven是集成的还是自己下载的,然后去配置setting.xml文件
- Python-老男孩-01_基础_文件IO_函数_yield_三元_常用内置函数_反射_random_md5_序列化_正则表达式_time
Python2.7 缩进统一: 约定 常量 大写 , 变量 小写 判断一个变量在内存中的地址,也能看出是不是一个值 id()函数 >>> x = 'abc' >>&g ...
- Activiti常见问题解决
1,工作流activiti eclipse 插件不自动生成png window ——> preferences——>activiti——>save——>选中create pro ...
- 去掉 Warning:$HADOOP_HOME is deprecated
修改配置文件/etc/profile,增加环境变量HADOOP_HOME_WARN_SUPPRESS=1, 保存退出,再次启动hadoop,就不会出现警告信息了
- angular学习笔记01
angular.js路由功能 用于实现单页应用 //html 代码 <div ng-view></div> //js代码 angular.module('myM1',['ng' ...
- oracle pl/sql 控制结构(分支,循环,控制)
一.pl/sql的进阶--控制结构在任何计算机语言(c,java,pascal)都有各种控制语句(条件语句,循环结构,顺序控制结构...),在pl/sql中也存在这样的控制结构.在本部分学习完成后,希 ...
- 【个人笔记】《知了堂》express模块
NPM 包管理器 Node package module ==>简称npm 类似的bower 安装express 1.全局Npm install express -g 2.项目中安装 项目中 ...
- program 1 : python codes for login program(登录程序python代码)
#improt time module for count down puase time import time #set var for loop counting counter=1 #logi ...
- win32多线程编程
关于多线程多进程的学习,有没有好的书籍我接触的书里头关于多线程多进程部分,一是<操作系统原理>里面讲的相关概念 一个是<linux基础教程>里面讲的很简单的多线程多进程编程 ...