[抄题]:

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;
}
}

最新文章

  1. [I2C]I2C总线协议图解
  2. Javascript算术运算
  3. Python基础 - 获取N天前的日期
  4. Mybatis中的collection、association来处理结果映射
  5. 一、Android应用程序的基本原理(Fundamentals [,fʌndə&#39;mentlz])
  6. BZOJ 3944 Sum 解题报告
  7. SQL用replace替换文本部分内容
  8. ssm框架理解
  9. java设计模式之二抽象工厂模式(Abstract Factory)
  10. Excel图表-太极图
  11. (14)CountTriplets
  12. 如何在linux下开启FTP服务
  13. centos mysql 实战 第一节课 安全加固 mysql安装
  14. networkManger介绍
  15. Linux命令中:rsync和cp之间的区别
  16. bacula备份终端操作bconsole指令
  17. C++异常抛出与捕获及处理
  18. 什么叫做hack
  19. noip200806火柴棒等式
  20. MySQL数据库(二)

热门文章

  1. (十二)break,continue
  2. Java进阶知识点4:不可变对象与并发 - 从String说起
  3. C语言内存对齐对则
  4. 剑指offer-第五章总结
  5. C#检测应用程序重复启动----函数检测(可以在多用户登录情况下检测)
  6. 10 结构体和类 - —— 《Swift3.0 从入门到出家》
  7. Servlet概念入门
  8. CENTOS7配置静态IP后无法ping通外部网络的问题
  9. php end()
  10. Linux Shell 1&gt;/dev/null 2&gt;&amp;1 含义