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 ...
随机推荐
- 【51nod-1596】搬货物
现在有n个货物,第i个货物的重量是 2wi .每次搬的时候要求货物重量的总和是一个2的幂.问最少要搬几次能把所有的货物搬完. 样例解释: 1,1,2作为一组. 3,3作为一组. Input 单组测试数 ...
- ARM汇编指令集5
为什么需要多寄存器访问指令? ldr/str每周期只能访问4字节内存,如果需要批量读取.写入内存时太慢,解决方案是stm/ld 举例(uboot start.S 537行) stmia sp, ...
- Linux命令四
作业一: 1) 开启Linux系统前添加一块大小为20G的SCSI硬盘 2) 开启系统,右击桌面,打开终端 安装的是命令行界面 3) 为新加的硬盘分区,一个主分区大小为10G,剩余空间给扩展分区,在扩 ...
- LeetCode OJ:Majority Element(主要元素)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- NSPredicate(正则表达式)
1. 正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串.通常被用来检索.替换那些符合某个模式的文本. 2. iOS中正则使用 有三种(NSPredicate, rangeOfStri ...
- git合并分支与解决冲突
前提: 当前开发的分支为feature/20161129_317606_algoplatform_1,由于feature/20161130_322574_tmstools_1分支有新内容,所以准备将f ...
- svn的使用流程
一.安装: 1. 服务器端:VisualSVN_Server 2. 客户端:TortoiseSVN 二.使用VisualSVN Server建立版本库 1. 首先打开VisualSVN Server ...
- 有状态与无状态 cookie session
服务器所维护的与客户交互活动的信息称为状态信息.不保存任何状态信息的服务器称为无状态服务器(stateless server),反之则称为有状态服务器(stateful server). 面向连接对应 ...
- innerHTML和innerText怎么区分
示例代码:<div id="test"> <span style="color:red">test1</span> test ...
- vscode使用Markdown文档编写
首先安装vscode工具,具体的使用可以参考之前的博文:<Visual Studio Code教程:基础使用和自定义设置> VScode已经默认集成markdown文档编辑插件.可以新建一 ...