public class Solution {
public int FindLHS(int[] nums) {
Dictionary<int, int> dic = new Dictionary<int, int>();
foreach (var num in nums)
{
if (!dic.ContainsKey(num))
{
dic.Add(num, );
}
else
{
dic[num]++;
}
} var list = dic.OrderBy(x => x.Key).ToList();
if (list.Count == )
{
return ;
}
else
{
var lastkey = ;
var lastLen = ;
var max = ;
for (int i = ; i < list.Count; i++)
{
if (i == )
{
lastkey = list[i].Key;
lastLen = list[i].Value;
continue;
}
else
{
var curKey = list[i].Key;
var curLen = list[i].Value;
if (curKey - lastkey == )
{
var totalLen = lastLen + curLen;
if (max < totalLen)
{
max = totalLen;
}
}
lastkey = curKey;
lastLen = curLen;
}
}
return max;
}
}
}

https://leetcode.com/problems/longest-harmonious-subsequence/#/description

leetcode594的更多相关文章

  1. [Swift]LeetCode594. 最长和谐子序列 | Longest Harmonious Subsequence

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

随机推荐

  1. java jprofile

    java -agentpath:/opt/jprofiler8/bin/linux-x64/libjprofilerti.so=port=8849,nowait -Xdebug -Xrunjdwp:t ...

  2. Idea_00_资源贴

    一.精选资料 tengj/IntelliJ-IDEA-Tutorial IntelliJ IDEA 使用教程-极客学院 二.参考资料 eclipse&Myeclipse&Intelli ...

  3. LeetCode OJ:Sqrt(x)(平方根)

    Implement int sqrt(int x). Compute and return the square root of x. 简单的二分法,注意mid应该选为long,否则容易溢出: cla ...

  4. Qt5学习笔记——QRadioButton与QbuttonGroup

    [我是小标题:使用QToolButton实现radio button功能.] QRadioButton是什么?  下图是Windows系统中典型的radio button显示效果.    QRadio ...

  5. git配置ssh key并从github.com拉取repos

    一.配置ssh key 1. 进入当前用户目录cd ~2. 生成ssh keyssh-keygen -t rsa -C "ABC@qq.com"ABC@qq.com账号必须是你登录 ...

  6. Java进阶面试问题列表

    面向对象编程的基本理念与核心设计思想 解释下多态性(polymorphism),封装性(encapsulation),内聚(cohesion)以及耦合(coupling). 继承(Inheritanc ...

  7. Android spannableStringBuilder用法整理

    Android spannableStringBuilder用法整理 分类: Android开发2013-11-29 10:58 5009人阅读 评论(0) 收藏 举报 Androidspannabl ...

  8. 【spring源码学习】spring的IOC容器之BeanFactoryPostProcessor接口学习

    [一]org.springframework.beans.factory.config.BeanFactoryPostProcessor接口==>该接口实现方法的执行时机:该接口void pos ...

  9. MDK中STM32使用Printf函数详细解析【转载】

    在用MDK调试STM32板子串口时,为了方便串口调试,调用了printf()函数,用Keil仿真是,串口不能正确的输出,软件仿真时,总是卡在那 里.有点纳闷,然后调用USART_SendData()函 ...

  10. 遍历listmap 遍历map

    package excel; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import j ...