Question

594. Longest Harmonious Subsequence

Solution

题目大意:找一个最长子序列,要求子序列中最大值和最小值的差是1。

思路:构造一个map,保存每个元素出现的个数,再遍历这个map,算出每个元素与其邻元素出现的次数和,并找到最大的那个数

Java实现:

public int findLHS(int[] nums) {
if (nums == null || nums.length == 0) return 0;
Map<Integer, Integer> map = new HashMap<>();
for (int tmp : nums) {
Integer cnt = map.get(tmp);
if (cnt == null) cnt = 0;
map.put(tmp, cnt + 1);
}
int count = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
int before = map.get(entry.getKey() - 1) != null ? map.get(entry.getKey() - 1) : -entry.getValue();
int next = map.get(entry.getKey() + 1) != null ? map.get(entry.getKey() + 1) : -entry.getValue();
if (before + entry.getValue() > count) count = before + entry.getValue();
if (next + entry.getValue() > count) count = next + entry.getValue();
}
return count;
}

594. Longest Harmonious Subsequence - LeetCode的更多相关文章

  1. 【Leetcode_easy】594. Longest Harmonious Subsequence

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

  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. 594. Longest Harmonious Subsequence强制差距为1的最长连续

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

  6. 594. Longest Harmonious Subsequence

    方法一:用一个map来辅助,代码简单,思路清晰 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }( ...

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

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

  8. LeetCode Longest Harmonious Subsequence

    原题链接在这里:https://leetcode.com/problems/longest-harmonious-subsequence/description/ 题目: We define a ha ...

  9. C#LeetCode刷题之#594-最长和谐子序列​​​​​​​​​​​​​​(Longest Harmonious Subsequence)

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

随机推荐

  1. python学习笔记(一)——Python基础

    一.python 基础语法 python 的解释器在启动时会自动加载一个内建的模块,因此我们在使用 print().input()等函数时不用导入其他模块文件. 基本语法: 每条语句结尾没有分号 定义 ...

  2. Numpy实现机器学习交叉验证的数据划分

    Numpy实现K折交叉验证的数据划分 本实例使用Numpy的数组切片语法,实现了K折交叉验证的数据划分 背景:K折交叉验证 为什么需要这个?在机器学习中,因为如下原因,使用K折交叉验证能更好评估模型效 ...

  3. 解决 css 浮动后 父元素高度失效问题

    应用场景 子元素标签使用 浮动后,会出现浮在父元素上层,脱离了.导致父元素没办法根据子元素的高度而变化,提供以下解决方案. 解决代码 把 '.clearfix ' Class 样式添加到 父元素即可. ...

  4. canvas系列教程03-柱状图项目1

    弄了一堆线方块和函数,感觉挺玄乎,然并卵.我们直接写个项目看看. canvas一个比较拽的应用就是图表和游戏,我们从浅入深,先玩图表,图表我们今天说一个最简单柱状图. 柱状图很多人用百度的echars ...

  5. UML与面向对象程序设计原则

    [实验任务一]:UML复习 阅读教材第一章复习UML,回答下述问题: 面向对象程序设计中类与类的关系都有哪几种?分别用类图实例说明. 1.  关联关系 (1)  双向关联 (2)  单向关联 (3)  ...

  6. PAT A1001 A+B Format

    Calculate a+b and output the sum in standard format -- that is, the digits must be separated into gr ...

  7. 搭建 SpringBoot 项目(前端页面 + 数据库 + 实现源码)

    SpringBoot 项目整合源码 SpringBoot 项目整合 一.项目准备 1.1 快速创建 SpringBoot 项目 1.2 项目结构图如下 1.3 数据表结构图如下 1.4 运行结果 二. ...

  8. java基础-File

    File类 * File更应该叫做一个路径, 文件路径或者文件夹路径    * 路径分为绝对路径和相对路径  * 绝对路径是一个固定的路径,从盘符开始  * 相对路径相对于某个位置,在eclipse下 ...

  9. localStorage存储返回过来的对象 显示object object的问题

    localStorage.setItem() 不会自动将Json对象转成字符串形式 用localStorage.setItem()正确存储JSON对象方法是: 存储前先用JSON.stringify( ...

  10. centos6的yum源更新版本

    概述 centos6系统从2020年12月1号开始不再维护,官方的yum源不再可用,同时国内的阿里云镜像和163镜像也都不再可用. 但是我们有一些老的服务器仍然在使用centos6系统版本,依赖库的安 ...