【Lintcode】1880. Largest Number X Which Occurs X Times
题目地址:
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相关推荐
- 【Lintcode】046.Majority Number
题目: Given an array of integers, the majority number is the number that occurs more than half of the ...
- 【转】oracle数据库NUMBER数据类型
原文:http://www.jb51.net/article/37633.htm NUMBER ( precision, scale)a) precision表示数字中的有效位;如果没有指定prec ...
- mysql的number类型对应的db2_【转】oracle数据库NUMBER数据类型
NUMBER ( precision, scale)a) precision表示数字中的有效位;如果没有指定precision的话,Oracle将使用38作为精度. b) 如果scale大于零,表 ...
- 【LintCode】算法题 1443. 最长AB子串
描述 给你一个只由字母'A'和'B'组成的字符串s,找一个最长的子串,要求这个子串里面'A'和'B'的数目相等,输出该子串的长度. 这个子串可以为空. s的长度n满足 2<=n<=1000 ...
- 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连
解析 max ksubarray sum: 最大和 of 连续子序列 => 最大和 of k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff: 无queu ...
- 【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 ...
- 【Lintcode】1645. Least Subsequences
题目地址: https://www.lintcode.com/problem/1645/ 给定一个长nnn的数组AAA,问AAA最少能分解为多少个严格递减的子序列之并. 在AAA上定义偏序关系< ...
- 【Lintcode】1375. Substring With At Least K Distinct Characters
题目地址: https://www.lintcode.com/problem/substring-with-at-least-k-distinct-characters/description 给定一 ...
- 【Lintcode】1647. Path Search
题目地址: https://www.lintcode.com/problem/path-search/description 给定一个无向图,再给定两个点SSS和TTT,求所有SSS到TTT的简单路径 ...
- 【Lintcode】1723. Shortest Path in a Grid with Obstacles Elimination
题目地址: https://www.lintcode.com/problem/shortest-path-in-a-grid-with-obstacles-elimination/descriptio ...
最新文章
- c语言迭代法求平方根_求平方根问题 (C++ 实现)
- 华为怎么用手机看时间到读秒_华为手机灭屏也可以看时间?其实设置方法很简单,不会有些可惜了...
- 数学与算法《文档相似性 - 余弦定理》
- 深度学习笔记(32) 目标定位
- C语言头文件尖括号和双引号的区别
- 解决ssh登录慢,等待时间长的问题
- 零散的JavaScript公用方法
- KITTI 数据集简介
- 为猿七年有余,痒否?痛否?
- SourceOffSite Server和Visual SourceSafe 6.0d锁死问题的解决
- 服务器远程关机后开机开不了,远程开关机系统安全吗
- Python:实现测试信用卡号码有效性credit card validator的算法(附完整源码)
- 医学检验质量管理和控制计算机的,【临床医学论文】临床医学检验技术质量管理现状分析(共4169字)...
- 脑与认知科学1 脑的基本结构上
- [搞笑]CPU煎蛋法
- Android支持的视频格式
- 【SqlServer】JDBC驱动下载地址
- 建筑工程施工数字化管理平台特点
- 深入理解搜索引擎——搜索评价指标
- python 基类是什么_python之抽象基类