LeetCode 594. Longest Harmonious Subsequence

考点 难度
Array Easy
题目

Alice has n candies, where the ith candy is of type candyType[i]. Alice noticed that she started to gain weight, so she visited a doctor.

The doctor advised Alice to only eat n / 2 of the candies she has (n is always even). Alice likes her candies very much, and she wants to eat the maximum number of different types of candies while still following the doctor’s advice.

Given the integer array candyType of length n, return the maximum number of different types of candies she can eat if she only eats n / 2 of them.

思路

看到题首先想到计算每个数字出现的次数,从次数最多的数字n往下排,找n-1或者n+1有没有出现,选出现次数更多的一个,以此类推。
solution里面的【Approach 4: Using HashMap】和这种方法很像,但是码更简单。主要区别在最后一步。
另外在建map的时候可以用getOrDefault( )快速查找。

答案

按照solution的方法改之后答案和solution提供的很像

public int findLHS(int[] nums) {HashMap <Integer, Integer> count = new HashMap<>();for (int num: nums){count.put(num, count.getOrDefault(num, 0) + 1);}int len = 0;for (int key: count.keySet()){if (count.containsKey(key + 1)){len = Math.max(len, count.get(key) + count.get(key + 1));}}return len;}

Leet Code知识点总结 - 594相关推荐

  1. Leet Code 力扣 - - 最短最优雅python解法带解析汇总

    Leet Code 刷题笔记 - - 不求最快最省,但求最短最优雅 前言 代码精炼是 Python 的核心,同时能够反应对于语言的熟练程度,本项目目的在于汇总 leet code 最短最优雅的解法,拒 ...

  2. Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...

  3. Leet Code题解 - 1559. Detect Cycles in 2D Grid 检测二维无向图中的环

    Leet Code题解 -- 1559. Detect Cycles in 2D Grid 检测二维无向图中的环 前言 一.题目描述 二.思路整理 1. 审题 2. 分布实现步骤 2.1 将二维数组处 ...

  4. leet code: Two Sum

    leet code: Two Sum 题目 Given an array of integers, return indices of the two numbers such that they a ...

  5. Leet Code OJ 刷题顺序参考

    出现频度为5:  1. Leet Code OJ 1. Two Sum [Difficulty: Easy]  2. Leet Code OJ 8. String to Integer (atoi) ...

  6. 案例1:金融数据分析----code知识点详解版

    案例1:金融数据分析----code详解版 1.引言 1.1案例分析目标 1.2涉及知识点 1.3案例分析流程 2.数据获取 `涉及知识点:` 2.1安装*tushare*库 2.2获取Token 2 ...

  7. java遍历斐波纳契数列_详解循环、迭代、递归、分治(Leet Code 509 斐波那契数列),实际运用...

    Multiple solutions of Fibonacci (Python or Java) 本章是用英文写的,作为或想成为一名优秀的攻城狮,习惯阅读英文文档将使你受益良多.例如更好的查看最新版的 ...

  8. Leet Code OJ 简单(二)

    20. 有效括号  48ms class Solution:def isValid(self, s):""":type s: str:rtype: bool"& ...

  9. Leet Code之Number of 1 bits

    常规方法,一位一位右移,算出1的个数.但是最大的数有32个1,需要右移32次,效率较低. 常规方法: 1 int hammingWeight(uint32_t n) { 2 int k,num; 3 ...

最新文章

  1. java 运算符表达式
  2. Echarts图表在VUE项目中使用动态数据源
  3. 【STM32】定时器中断实验代码详解
  4. gcc编译C++程序
  5. 深度探索I/O完成端口
  6. FastDFS单机版安装教程
  7. 教你榨干保险公司最后一滴血
  8. linux nfs文件共享
  9. Atitit 提升科技影响力 目录 1. 大原则 2 1.1. 科技强人必须是创新型[ 2 1.2. 要有一定的体量和规模 2 2. 创新能力主要科技指标 2 2.1. 领域科技大奖与荣誉 2 2
  10. 设计模式之单例模式:7种单例设计模式(Java实现)
  11. [项目管理-6]:软硬件项目管理 - 项目沟通管理(渠道、方法)
  12. 如何让Ubuntu联网
  13. matlab加权网络图,如何在matlab中编写求解加权网络的各个节点..._网络编辑_帮考网...
  14. 叶俊:从一匹马的电影读懂人才管理与商业经营
  15. jQuery 案例-图片抽奖
  16. 学术-数学:四色猜想
  17. 技术大佬:我去,你竟然还不会用 this 关键字
  18. 看电影的计算机配置,2160p的电影需要什么电脑配置看才流畅?
  19. mysql语句group by用法详解
  20. HTTP错误 404.17 - Not Found IIS 7.5 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理

热门文章

  1. 实现北京公交实时预警小程序
  2. 使用kNN算法对魔方颜色进行分类
  3. 鸿蒙为什么翻译成harmony,鸿蒙的英文名为什么取的是Harmony
  4. 北京/深圳招聘 | 华为诺亚方舟实验室招聘决策推理方向实习生、博士后等
  5. MAC电脑 查看APK文件的包名
  6. java如何处理tcp异常断开_针对TCP连接异常断开的分析
  7. 微软应用商店_应用不够网页来凑 微软商店将支持PWA网络应用 尽管这没太大的意义...
  8. cs230 deeplearning.ai 3: RNN
  9. 合同审查自动化-企业合同处理新模式
  10. HBDDR-123型时间继电器