[抄题]:

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].

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

max1 + max2 最大

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

差值必须是1 是0不行。所以[11111]输出为0。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

熟悉hashmap的一些不常用方法,剩下就是语言表达了

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

占空间但是好用,没别的办法了那就占吧

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int findLHS(int[] nums) {
//cc
if (nums == null || nums.length == 0) {
return 0;
} //ini: put into hashmap
Map<Integer,Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int max = 0; //for loop, getmax
for (int key : map.keySet()) {
if (map.containsKey(key + 1)) {
max = Math.max(max, map.get(key) + map.get(key + 1));
}
} //return max
return max;
}
}

594. Longest Harmonious Subsequence强制差距为1的最长连续的更多相关文章

  1. 【Leetcode_easy】594. Longest Harmonious Subsequence

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

  2. 594. Longest Harmonious Subsequence - LeetCode

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

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

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

  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. 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)

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

  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. [Swift]LeetCode594. 最长和谐子序列 | Longest Harmonious Subsequence

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

  9. LeetCode Longest Harmonious Subsequence

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

随机推荐

  1. [QT]问题记录-控件初始化导致程序异常关闭

    qt新手,在设置 pushButton 的字体颜色时,出现软件异常闭,代码如下: 按钮的初始化在  ui->setupUi(this); 前边,会出现一下问题. 解决办法:将按钮的初始化在  u ...

  2. linux基础【文件夹含义】

    linux文件目录是一个树状的目录 bin -->可执行文件 boot-->操作系统引导文件,系统内核,启动信息 dev -->device,设备信息,计算机硬件设备信息 etc - ...

  3. docx转doc时,防止公式被转成图片的解决办法

    编辑社回复需要doc(Word 97-2003)格式的文档,可是将docx(Word 2007+)另存为doc格式时,发现公式被转成了图片.其实,最简单的办法就是,打个电话过去给编辑社:“大爷,拜托您 ...

  4. Apache中 RewriteCond 规则参数介绍 转

    摘要: RewriteCond指令定义了规则生效的条件,即在一个RewriteRule指令之前可以有一个或多个RewriteCond指令.条件之后的重写规则仅在当前URI与Pattern匹配并且满足此 ...

  5. Freemarker 自定义标签 实现TemplateDirectiveModel

    1 自定义标签需要实现TemplateDirectiveModel这个接口中的execute方法 实例代码如下 public class UserListDirective implements Te ...

  6. 1080. MOOC期终成绩 (25)

    对于在中国大学MOOC(http://www.icourse163.org/)学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的在线编程作业分,然后总评获得不少于60分(满 ...

  7. Linux环境安装git

    git配置 源码安装 检测当前git版本是否是2.7.4以上 git --version 如果没有安装git直接源码安装即可,如果安装了先删除原来的git. yum -y remove git 先安装 ...

  8. spring的@Transactional注解详细用法(转)

    概述 事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性.Spring Framework对事务管理提供了一致的抽象,其特点如下: 为不同的事务API提供一致的编程模型, ...

  9. (转)WebApi 上传文件

    本文转载自:http://www.cnblogs.com/zj1111184556/p/3494502.html public class FileUploadController : ApiCont ...

  10. java代码====实现按钮点击改变颜色=======

    总结: package com.a.b; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event ...