leetcode594
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的更多相关文章
- [Swift]LeetCode594. 最长和谐子序列 | Longest Harmonious Subsequence
We define a harmonious array is an array where the difference between its maximum value and its mini ...
随机推荐
- 通过IndexOf获得DataRow在DataTable中的行号
Row = dt.Rows.IndexOf(dr);
- LeetCode OJ:Jump Game(跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 在一个Activity中启动另一个Activity
一.新建一个空的工程 二.添加一个Activity并命名为BAty 三.在activity_main.xml中添加一个按钮,设置id号为btnStartB <Button android:lay ...
- Android 用户界面---定制组件(Custom Components)
基于布局类View和ViewGroup的基本功能,Android为创建自己的UI界面提供了先进和强大的定制化模式.首先,平台包含了各种预置的View和ViewGroup子类---Widget和layo ...
- 关于Object类的equals方法和hashCode方法
关于Object类的equals的特点,对于非空引用: 1.自反性:x.equals(x) return true : 2.对称性:x.equals(y)为true,那么y.equals(x)也为tr ...
- OC-SEL 和 Class
[认识选择器]============================================ SEL 1.选择器(SEL)是一个变量类型. 2.选择器用来装成员消息(成员方法) people ...
- 剑指offer--37.和为S的两个数字
链接:https://www.nowcoder.com/questionTerminal/390da4f7a00f44bea7c2f3d19491311b来源:牛客网@华科渣硕 不要被题目误导了!证明 ...
- 给UIButton设置阴影及动画组
//设置“开启旅程”按钮 UIButton *startBtn = self.startBtn; CGFloat btnW = ; CGFloat btnH = ; CGFloat btnX = (s ...
- 3 循环语句——《Swift3.0从入门到出家》
3 循环语句 当一段代码被多次重复利用的使用我们就使用循环 swift提供了三种形式的循环语句 1.while 循环 2.repeat — while 循环 3.for — in 循环 while 循 ...
- Raid 技术简介
独立硬盘冗余阵列(RAID, Redundant Array of Independent Disks),旧称廉价磁盘冗余阵列,简称硬盘阵列.其基本思想就是把多个相对便宜的硬盘组合起来,成为一个硬盘阵 ...