本题题意:
在一个数组中,找一个这样的和谐数组,他们的最大值和最小值的差值正好是1.
Input: [1,3,2,2,5,2,3,7]
Output: 5
Explanation: The longest harmonious subsequence is [3,2,2,2,3].

就是可以还是不连续的。

第一遍写:

    Map<Integer, Integer>  dc = new HashMap<>();int res = 0;for (int i = 0; i < nums.length; ++i){if (dc.containsKey(nums[i]) )    continue;else{int tr = 1;boolean h = false;for (int j = i + 1; j < nums.length; ++j)if (nums[j] == nums[i])tr += 1;else if(nums[j] == nums[i] + 1){tr += 1;h = true;}res = Math.max(res, h ? tr: 0);tr = 1;h = false;for(int j = i + 1; j < nums.length; ++j)if (nums[j] == nums[i])tr += 1;else if(nums[j] == nums[i] - 1){tr += 1;h = true;}res = Math.max(res, h ? tr : 0);}}return res;

上面的思路是:用Hash记录,nums[i]的最大harmonious序列长度
下面修改hash,让他直接记录次数不是更好嘛,然后再算。
第二遍写:

    Map<Integer, Integer>  dc = new HashMap<>();for (int i = 0; i < nums.length; ++i)if (dc.containsKey(nums[i]))    dc.put(nums[i], dc.get(nums[i]) + 1);else    dc.put(nums[i], 1);int res = 0;for (int i = 0; i < nums.length; ++i){if (dc.containsKey(nums[i] - 1))   res = Math.max(res, dc.get(nums[i] - 1) + dc.get(nums[i]));if (dc.containsKey(nums[i] + 1))   res = Math.max(res, dc.get(nums[i] + 1) + dc.get(nums[i]));}return res;

然后又用python写了一遍.

def findLHS(self, nums: List[int]) -> int:dc = collections.Counter(nums)#nums[i] : countsres = 0for key, c in dc.items():if dc[key - 1] != 0:    res = max(res, dc[key] + dc[key - 1])if dc[key + 1] != 0:    res = max(res, dc[key] + dc[key + 1])return res

最后嘞,看答案感觉又菜鸡了,其实是不用判定key - 1的,为什么嘞?

  1. 因为只要他没有key - 1,这是当然不用判定的。
  2. 如果要是有key - 1,那么到了key - 1就算的是上一次的。一直到最后。
    然后,字典加入也有小技巧
dc.put(num, dc.getOrDefault(num, 0) + 1);

代码如下:

    Map<Integer, Integer>  dc = new HashMap<>();int res = 0;for (int num : nums)dc.put(num, dc.getOrDefault(num, 0) + 1);for (int num : nums)if (dc.containsKey(num + 1))   res = Math.max(res, dc.get(num + 1) + dc.get(num));   return res;

转载于:https://www.cnblogs.com/whyaza/p/10860590.html

594. Longest Harmonious Subsequence相关推荐

  1. LeetCode 594. Longest Harmonious Subsequence

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

  2. leetcode 594. Longest Harmonious Subsequence | 594. 最长和谐子序列

    题目 https://leetcode-cn.com/problems/longest-harmonious-subsequence/ 题解 我的解法 测试用例 [1] [1,2] [2,1] [3, ...

  3. leetcode (Longest Harmonious Subsequence)

    Title:Longest Harmonious Subsequence    594 Difficulty:Easy 原题leetcode地址: https://leetcode.com/probl ...

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

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

  5. HPU第三次积分赛-D:Longest Increasing Subsequence(DP)

    Longest Increasing Subsequence 描述 给出一组长度为n的序列,a1​,a2​,a3​,a4​...an​, 求出这个序列长度为k的严格递增子序列的个数 输入 第一行输入T ...

  6. [Swift]LeetCode522. 最长特殊序列 II | Longest Uncommon Subsequence II

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  7. [Swift]LeetCode873. 最长的斐波那契子序列的长度 | Length of Longest Fibonacci Subsequence...

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  8. [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  9. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

最新文章

  1. Scala类型系统——高级类类型(higher-kinded types)
  2. 结合不同的模型进行集成学习
  3. markdown 图片居中_Markdown常用语法汇总
  4. java进销存一个人要做多久,如何取得当前年的时间! 找java进销存系统
  5. VMware vSphere/vCenter/ESX(i)介绍
  6. GDCM:变更dcm文件的序列的测试程序
  7. 相亲有风险,且行且珍惜!| 今日最佳
  8. scratch项目-会走路的小猫
  9. Cheat Engine(CE)教程
  10. 8个问题看你是否真的懂 JS
  11. 小米商城网页制作(附源码)
  12. 微服务网关Gateway基本知识(一)
  13. Hi-Fi小经验(转)
  14. Spring Security入门到实践(一)HTTP Basic在Spring Security中的应用原理浅析
  15. python爬虫学习之爬取全国各省市县级城市邮政编码
  16. 三大步骤轻松搞定RTK:求转换参数、测量及放样、数据传输
  17. cad 二次开发 插入图片_CAD 二次开发 引用外部DWG并放到对应图层上
  18. 软工网络15结对编程练习 201521123056 吴剑通
  19. 英文关键词提取之RAKE算法
  20. 【Python】Python使用you-get库下载网址-公众号视频

热门文章

  1. 人脸识别经典开源项目
  2. Unity获取麦克风实现吹气球效果
  3. Android 11何时进入手机,以及如何安装?
  4. 超全软件下载网站和网页(一网一匠)
  5. 电脑怎么查看处理器CPU型号、属性以及显卡型号
  6. android气泡样式图片,Android实现三角形气泡效果方式汇总
  7. Sharepoint visio Web Access
  8. css中好看常用的中文字体
  9. android 锯齿
  10. 大屏自适应文章收藏分享