当前位置:网站首页>day10每日3题(2):统计最大组的数目
day10每日3题(2):统计最大组的数目
2022-06-26 16:17:00 【程序猿不脱发2】
题目:
给你一个整数 n 。请你先求出从 1 到 n 的每个整数 10 进制表示下的数位和(每一位上的数字相加),然后把数位和相等的数字放到同一个组中。
请你统计每个组中的数字数目,并返回数字数目并列最多的组有多少个。
示例 1:
输入:n = 13
输出:4
解释:总共有 9 个组,将 1 到 13 按数位求和后这些组分别是:
[1,10],[2,11],[3,12],[4,13],[5],[6],[7],[8],[9]。总共有 4 个组拥有的数字并列最多。
示例 2:
输入:n = 2
输出:2
解释:总共有 2 个大小为 1 的组 [1],[2]。
示例 3:
输入:n = 15
输出:6
示例 4:
输入:n = 24
输出:5
提示:
1 <= n <= 10^4
思路:
对于 [1, n] 中的每一个整数 i,我们可以计算出它的数位和 s_i 。建立一个从数位和到原数字的哈希映射,对每一个数字 i,使键 s_i 对应的值自增一。然后我们在值的集合中找到最大的值 m,再遍历哈希表,统计值为 m 的个数即可。
java代码:
class Solution {
public int countLargestGroup(int n) {
Map<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
int maxValue = 0;
for (int i = 1; i <= n; ++i) {
int key = 0, i0 = i;
while (i0 != 0) {
key += i0 % 10;
i0 /= 10;
}
hashMap.put(key, hashMap.getOrDefault(key, 0) + 1);
maxValue = Math.max(maxValue, hashMap.get(key));
}
int count = 0;
for (Map.Entry<Integer, Integer> kvpair : hashMap.entrySet()) {
if (kvpair.getValue() == maxValue) {
++count;
}
}
return count;
}
}
边栏推荐
- Ideal path problem
- Redis顺序排序命令
- (1) Keras handwritten numeral recognition and recognition of self written numbers
- 今年高考英语AI得分134,复旦武大校友这项研究有点意思
- Cookie和Session详解
- Mono 的一些实例方法
- Tsinghua's "magic potion" is published in nature: reversing stem cell differentiation, and the achievements of the Nobel Prize go further. Netizen: life can be created without sperm and eggs
- 当一个程序员一天被打扰 10 次,后果很惊人!
- 若依微服务特殊字符串被过滤的解决办法
- [从零开始学习FPGA编程-46]:视野篇 - 集成电路的发展与技术进步
猜你喜欢
Scala 基础 (二):变量和数据类型

对话长安马自达高层,全新产品将在Q4发布,空间与智能领跑日系

100+数据科学面试问题和答案总结 - 基础知识和数据分析

Dialogue with the senior management of Chang'an Mazda, new products will be released in Q4, and space and intelligence will lead the Japanese system

Supplement the short board - Open Source im project openim about initialization / login / friend interface document introduction

11 introduction to CNN

理想路径问题

TCP congestion control details | 1 summary

The details of the first pig heart transplantation were fully disclosed: human herpes virus was found in the patient, the weight of the heart doubled after death, and myocardial cell fibrosis
Practice of federal learning in Tencent micro vision advertising
随机推荐
Dialogue with the senior management of Chang'an Mazda, new products will be released in Q4, and space and intelligence will lead the Japanese system
Tsinghua's "magic potion" is published in nature: reversing stem cell differentiation, and the achievements of the Nobel Prize go further. Netizen: life can be created without sperm and eggs
Make up the weakness - Open Source im project openim about initialization / login / friend interface document introduction
C language reading data
『C语言』题集 of ⑩
mha 切换(操作流程建议)
【力扣刷题】11.盛最多水的容器//42.接雨水
基于Kubebuilder开发Operator(入门使用)
【207】Apache崩溃的几个很可能的原因,apache崩溃几个
油田勘探问题
JS教程之 使用 Electron.JS 构建原生桌面应用程序乒乓游戏
数据分析----numpy快速入门
首例猪心移植细节全面披露:患者体内发现人类疱疹病毒,死后心脏重量翻倍,心肌细胞纤维化丨团队最新论文...
Leetcode one week race 298, first three questions
R language plotly visualization: Violin graph, multi category variable violin graph, grouped violin graph, split grouped violin graph, two groups of data in each violin graph, each group accounts for
R语言plotly可视化:小提琴图、多分类变量小提琴图、分组(grouped)小提琴图、分裂的分组小提琴图、每个小提琴图内部分为两组数据、每个分组占小提琴图的一半、自定义小提琴图的调色板、抖动数据点
Solidus Labs欢迎香港前金融创新主管赵嘉丽担任战略顾问
Solidus labs welcomes zhaojiali, former head of financial innovation in Hong Kong, as a strategic adviser
100+数据科学面试问题和答案总结 - 基础知识和数据分析
心情不好,我就这样写代码