当前位置:网站首页>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;
}
}
边栏推荐
- 【从删库到跑路】JDBC 完结篇(一天学完系列!!学完赶紧跑!)
- 架构实战营毕业设计
- 牛客编程题--必刷101之动态规划(一文彻底了解动态规划)
- 【毕业季】致毕业生的一句话:天高任鸟飞,海阔凭鱼跃
- R language plot visualization: plot visualizes the normalized histogram, adds the density curve KDE to the histogram, and uses geom at the bottom edge of the histogram_ Adding edge whisker graph with
- Solidus Labs欢迎香港前金融创新主管赵嘉丽担任战略顾问
- Practice of federal learning in Tencent micro vision advertising
- R语言plotly可视化:小提琴图、多分类变量小提琴图、分组(grouped)小提琴图、分裂的分组小提琴图、每个小提琴图内部分为两组数据、每个分组占小提琴图的一半、自定义小提琴图的调色板、抖动数据点
- Structure the graduation project of actual combat camp
- Handwritten numeral recognition, run your own picture with the saved model
猜你喜欢

当一个程序员一天被打扰 10 次,后果很惊人!

今年高考英语AI得分134,复旦武大校友这项研究有点意思

Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)

首例猪心移植细节全面披露:患者体内发现人类疱疹病毒,死后心脏重量翻倍,心肌细胞纤维化丨团队最新论文...

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

大话领域驱动设计——表示层及其他

【力扣刷题】11.盛最多水的容器//42.接雨水

IAR engineering adapts gd32 chip

Natural language inference with attention and fine tuning Bert pytorch

Big talk Domain Driven Design -- presentation layer and others
随机推荐
C. Inversion Graph
【从删库到跑路】MySQL基础 完结篇(入个门先跑路了。。)
[graduation season] a word for graduates: the sky is high enough for birds to fly, and the sea is wide enough for fish to leap
心情不好,我就这样写代码
国内首款开源 MySQL HTAP 数据库即将发布,三大看点提前告知
【力扣刷题】11.盛最多水的容器//42.接雨水
基於Kubebuilder開發Operator(入門使用)
What is the process of switching C # read / write files from user mode to kernel mode?
Which position does Anxin securities rank? Is it safe to open an account?
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
Swiftui retrieves the missing list view animation
数据分析----numpy快速入门
大话领域驱动设计——表示层及其他
R language uses cor function to calculate the correlation matrix for correlation analysis, uses corrgram package to visualize the correlation matrix, reorders rows and columns using principal componen
2 三种建模方式
When a programmer is disturbed 10 times a day, the consequences are amazing!
"C language" question set of ⑩
最小二乘系统辨识课 中篇:递归最小二乘
JS教程之Electron.js设计强大的多平台桌面应用程序的好工具
Transformation of zero knowledge QAP problem