题目

https://leetcode-cn.com/problems/longest-harmonious-subsequence/

题解

我的解法


测试用例

[1]
[1,2]
[2,1]
[3,1]
[3,1,1]
[3,5,5]
[1,3,5,7]
[2,2]
[1,2,1,2,1,2,1,2,1,2]
[3,3,3,2,2,2,1,1,1]
[3,3,2]
[1,3,2,2,5,2,3,7]

import java.util.Arrays;class Solution {public int findLHS(int[] nums) {if (nums.length == 1) return 0;Arrays.sort(nums);int maxSum = 0;int numPre = Integer.MAX_VALUE;int cntPre = 0;int cntCur = 1;for (int i = 0; i < nums.length - 1; i++) {if (nums[i] == nums[i + 1]) {cntCur++;} else {if (numPre + 1 == nums[i]) { // update maxSum only when it's continuousmaxSum = Math.max(maxSum, cntPre + cntCur);}cntPre = cntCur;cntCur = 1;numPre = nums[i];}}// boundary: the last numberif (nums[nums.length - 2] != nums[nums.length - 1]) {cntPre = cntCur;cntCur = 1;}if (numPre + 1 == nums[nums.length - 1]) { // update maxSum only when it's continuousmaxSum = Math.max(maxSum, cntPre + cntCur);}return maxSum;}
}

评论区优雅解法

排序后,用两个指针,类似于窗口

class Solution {public int findLHS(int[] nums) {Arrays.sort(nums);int begin = 0,res = 0;for(int end = 0;end < nums.length;end++){while(nums[end] - nums[begin] > 1)begin++;if(nums[end] - nums[begin] == 1)res = Math.max(res,end - begin + 1);}return res;}
}

leetcode 594. Longest Harmonious Subsequence | 594. 最长和谐子序列相关推荐

  1. leetcode 300. Longest Increasing Subsequence | 300. 最长递增子序列(动态规划)

    题目 https://leetcode.com/problems/longest-increasing-subsequence/ 题解 难得有官方题解的一道题. 参考:https://leetcode ...

  2. leetcode 1143. Longest Common Subsequence | 1143. 最长公共子序列(动态规划,暴力递归->傻缓存->dp)

    题目 https://leetcode.com/problems/longest-common-subsequence/ 题解 经典的 暴力递归 -> 傻缓存 -> dp 题目,以 &qu ...

  3. leetcode (Longest Harmonious Subsequence)

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

  4. LeetCode 594. Longest Harmonious Subsequence

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

  5. 594. Longest Harmonious Subsequence

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

  6. leetcode(300)—— Longest Increasing Subsequence(最长递增子序列)

    参考 Python 解法: 动态规划 -- 最长递增子序列(LIS) 原题位置:Longest Increasing Subsequence | LeetCode OJ 题目的说明: 严格递增: 子序 ...

  7. leetcode 516. Longest Palindromic Subsequence | 516. 最长回文子序列(递归 -> 傻缓存 ->DP)

    题目 https://leetcode.com/problems/longest-palindromic-subsequence/ 题解 1.递归(超时) 递归 -> 傻缓存 ->DP c ...

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

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

  9. Leetcode 594. 最长和谐子序列 C++

    Leetcode 594. 最长和谐子序列 题目 和谐数组是指一个数组里元素的最大值和最小值之间的差别正好是1. 现在,给定一个整数数组,你需要在所有可能的子序列中找到最长的和谐子序列的长度. 示例: ...

最新文章

  1. makefile使用宏及用法$(宏标识符) $(cc)_宏编程的艺术
  2. WebBrowser加载完毕后再往下执行
  3. hive 中某个字段等于0_快速了解hive
  4. [算法笔记]分块算法从入门到TLE
  5. spark 的RDD各种转换和动作
  6. fastjson 序列化 不包括转义字符_fastjson漏洞复现
  7. cocos2d cocostudio
  8. python 官方 中文 文档
  9. linux合并ts文件吗,UNIX LINUX MACOS shell 下载合并*.ts视频
  10. 难崩日记——从入门到入土的求生之路(二):文件上传中的路径问题
  11. Python 三大神器:pip,virtualenv(virtualenvwrapper),fabric
  12. 给销售组织分配分销渠道
  13. 计算机科学与技术双一流排名,计算机科学与技术学科排行榜(大学名单大全2020版)...
  14. C# 使用Microsoft Speech朗读文本
  15. 霹雳吧啦Wz语义分割学习笔记P11
  16. 来自腾讯的高性能服务器架构思路
  17. 中文分词-转载3_一个北京程序员
  18. TCL电子上半年收入同比增103.7%;中芯国际二季度营收同比大增43%;友成基金会与亚马逊中国联合开展公益项目 | 全球TMT...
  19. 轻松快捷的安装Testlink,终于可以轻松搞定!
  20. HR面试经验总结 | HR面试常问问题

热门文章

  1. CodeForces - 1316D Nash Matrix(构造+dfs)
  2. POJ - 2516 Minimum Cost(最小费用最大流)
  3. 中石油训练赛 - 01 Matrix(构造)
  4. Jupyter Notebook导入自定义模块
  5. osqa java_从LSM-Tree、COLA-Tree谈到StackOverflow、OSQA(召唤前端)
  6. python扫描局公网ip_公网IP检测(python)
  7. java jfreechar鱼刺图_java使用jfreechar绘制饼型统计图
  8. python中strip是什么意思啊_Python中的strip()的理解
  9. 【数据结构】线性表的顺序存储结构(c语言实现)
  10. TCP keepalive的详解(解惑)