题目地址:

https://www.lintcode.com/problem/largest-number-x-which-occurs-x-times/description

给定一个数组,求本身数值与其出现次数相等的那些数里最大的那个。用哈希表即可。代码如下:

import java.util.HashMap;
import java.util.Map;public class Solution {/*** @param arr: an array of integers* @return: return the biggest value X, which occurs in A exactly X times.*/public int findX(int[] arr) {// write your code hereif (arr == null || arr.length == 0) {return 0;}Map<Integer, Integer> map = new HashMap<>();for (int x : arr) {map.put(x, map.getOrDefault(x, 0) + 1);}int res = 0;for (Map.Entry<Integer, Integer> entry : map.entrySet()) {int k = entry.getKey(), v = entry.getValue();if (k == v) {res = Math.max(res, k);}}return res;}
}

时空复杂度O(n)O(n)O(n)。

【Lintcode】1880. Largest Number X Which Occurs X Times相关推荐

  1. 【Lintcode】046.Majority Number

    题目: Given an array of integers, the majority number is the number that occurs more than half of the ...

  2. 【转】oracle数据库NUMBER数据类型

    原文:http://www.jb51.net/article/37633.htm NUMBER ( precision, scale)a)  precision表示数字中的有效位;如果没有指定prec ...

  3. mysql的number类型对应的db2_【转】oracle数据库NUMBER数据类型

    NUMBER ( precision, scale)a)  precision表示数字中的有效位;如果没有指定precision的话,Oracle将使用38作为精度. b)  如果scale大于零,表 ...

  4. 【LintCode】算法题 1443. 最长AB子串

    描述 给你一个只由字母'A'和'B'组成的字符串s,找一个最长的子串,要求这个子串里面'A'和'B'的数目相等,输出该子串的长度. 这个子串可以为空. s的长度n满足 2<=n<=1000 ...

  5. 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连

    解析 max ksubarray sum:  最大和 of 连续子序列 =>   最大和 of  k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff:  无queu ...

  6. 【leetcode】1090. Largest Values From Labels

    题目如下: We have a set of items: the i-th item has value values[i] and label labels[i]. Then, we choose ...

  7. 【Lintcode】1645. Least Subsequences

    题目地址: https://www.lintcode.com/problem/1645/ 给定一个长nnn的数组AAA,问AAA最少能分解为多少个严格递减的子序列之并. 在AAA上定义偏序关系< ...

  8. 【Lintcode】1375. Substring With At Least K Distinct Characters

    题目地址: https://www.lintcode.com/problem/substring-with-at-least-k-distinct-characters/description 给定一 ...

  9. 【Lintcode】1647. Path Search

    题目地址: https://www.lintcode.com/problem/path-search/description 给定一个无向图,再给定两个点SSS和TTT,求所有SSS到TTT的简单路径 ...

  10. 【Lintcode】1723. Shortest Path in a Grid with Obstacles Elimination

    题目地址: https://www.lintcode.com/problem/shortest-path-in-a-grid-with-obstacles-elimination/descriptio ...

最新文章

  1. c语言迭代法求平方根_求平方根问题 (C++ 实现)
  2. 华为怎么用手机看时间到读秒_华为手机灭屏也可以看时间?其实设置方法很简单,不会有些可惜了...
  3. 数学与算法《文档相似性 - 余弦定理》
  4. 深度学习笔记(32) 目标定位
  5. C语言头文件尖括号和双引号的区别
  6. 解决ssh登录慢,等待时间长的问题
  7. 零散的JavaScript公用方法
  8. KITTI 数据集简介
  9. 为猿七年有余,痒否?痛否?
  10. SourceOffSite Server和Visual SourceSafe 6.0d锁死问题的解决
  11. 服务器远程关机后开机开不了,远程开关机系统安全吗
  12. Python:实现测试信用卡号码有效性credit card validator的算法(附完整源码)
  13. 医学检验质量管理和控制计算机的,【临床医学论文】临床医学检验技术质量管理现状分析(共4169字)...
  14. 脑与认知科学1 脑的基本结构上
  15. [搞笑]CPU煎蛋法
  16. Android支持的视频格式
  17. 【SqlServer】JDBC驱动下载地址
  18. 建筑工程施工数字化管理平台特点
  19. 深入理解搜索引擎——搜索评价指标
  20. python 基类是什么_python之抽象基类

热门文章

  1. 台式计算机有乱码如何解决,电脑乱码怎么办,开机乱码解决方法
  2. 程序化广告(2):程序化广告的参与者
  3. 电脑各个硬件的理论知识 经典
  4. 卷积神经网络:一个模块化视角
  5. 全栈python之路——三篇文章带你踏入python大门-基础01
  6. 激光清洗铝合金氧化层的优势
  7. 大数据创造智慧城市的未来之光!
  8. Android系统开机画面修改
  9. linux 打开dmg文件_如何在Windows中打开DMG文件
  10. python 象棋 算法_象棋AI算法(二)