题目:
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].

Note: The length of the input array will not exceed 20,000.

思路:
就是要计算从vector中取数,组成一个子vector,这具子vector的最大数比最小数最大只能大1,不要求顺序。我的思路是先排序,将所有数放到map里来计算。具体思路见注释~

代码:

class Solution {
public:int findLHS(vector<int>& nums) {sort(nums.begin(),nums.end());//先排序int len=nums.size();//计算nums的长度,如果为0直接返回0if(len<1){return 0;}map<int,int> res;for(int i=0;i<len;++i){//将所有数放到一个map里,即每个数出现的次数知道了res[nums[i]]++;}map<int, int>::iterator iter1 = res.begin();//iter1从第一个元素开始,iter2从第二个开始map<int, int>::iterator iter2 = iter1;++iter2;int ret = 0;for (; iter2 != res.end(); iter1++, iter2++){if (res.count(iter1->first+1)){//如果前一个map元素对应的first的值+1,也在map中有ret = max(ret, iter1->second + iter2->second);//那么ret就取ret和连着两个map元素的second}                                                 //因为second存的是这个数出现的次数}return ret;    }
};

输出结果: 112ms

LeetCode 594. Longest Harmonious Subsequence相关推荐

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

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

  2. leetcode (Longest Harmonious Subsequence)

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

  3. 594. Longest Harmonious Subsequence

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

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

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

  5. 【暴力】LeetCode 300. Longest Increasing Subsequence

    LeetCode 300. Longest Increasing Subsequence Solution1:我的答案 暴力搜索,时间复杂度O(n2)O(n2)O(n^2) class Solutio ...

  6. 【动态规划 回文串11】LeetCode 516. Longest Palindromic Subsequence

    LeetCode 516. Longest Palindromic Subsequence 本博客转载自:http://www.cnblogs.com/grandyang/p/6493182.html ...

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

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

  8. LeetCode 521. Longest Uncommon Subsequence I

    题目: Given a group of two strings, you need to find the longest uncommon subsequence of this group of ...

  9. [leetcode] 300. Longest Increasing Subsequence (Medium)

    题意: 求最长增长的子序列的长度. 思路: 利用DP存取以i作为最大点的子序列长度. Runtime: 20 ms, faster than 35.21% of C++ online submissi ...

最新文章

  1. celery 停止执行中 task
  2. Java文件的状态_java中文件的状态
  3. 《图说VR入门》——DeepoonVR的大鹏(陀螺仪)枪
  4. JavaEE 设计模式
  5. 『设计模式』以为是个王者,后来班主任来了!设计模式--二五仔的观察者模式
  6. dp打开思路2:POJ2533 HDU1114 HDU1260 HDU1160(水题不水)
  7. [CF671E] Organizing a Race
  8. Vue 3.0已进入发布候选阶段!
  9. Vue多组件切换,并相互传值(在created和mounted生命周期钩子函数中渲染数据的区别)
  10. 统计挖掘那些事(八)—— 分层抽样与交叉验证
  11. 博科brocade光纤交换机alias-zone的划分--实操案例
  12. curl: symbol lookup error: curl: undefined symbol: curl_mime_free
  13. Suricata默认规则集相关
  14. 脱单盲盒小程序源码_微信脱单盲盒源码_交友盲盒系统源码
  15. 修改 docker容器hosts文件
  16. 微信小程序登录小实例
  17. python小玩意——抠图换背景
  18. 4.5 路径MTU发现
  19. JVM常见命令之jstack
  20. Step7编程语言编程概述结构

热门文章

  1. python 生成html_Python使用pyh生成HTML文档的方法示例
  2. C++:传递数组给函数
  3. C++:构造函数之初始化列表
  4. python __getitem__()方法==>可以直接通过P[key]做运算
  5. linux 下后台运行python脚本
  6. vue.js 重定向 和 404 等等相关的问题?
  7. js一个按钮弹出两个按钮_车内常见按钮,你却一个不认识?一分钟带你认识车内常见按钮...
  8. python封装举例_Python+Pycharm—学习1—封装导入
  9. 黑色精美Mxone电影先生在线影视网站源码 苹果cmsV10模板
  10. vscode 结束_21 个VSCode 快捷键,让代码更快,更有趣