Java 如何找到整数中重复次数最多的最高数
例如,如果 整数=30530 它必须返回3` 这是我尝试过的,但我想不起来了,我不知道我在哪里丢失了它,如果有其他方法可以不用转换成字符串,我也会很感激Java 如何找到整数中重复次数最多的最高数,java,Java,例如,如果 整数=30530 它必须返回3` 这是我尝试过的,但我想不起来了,我不知道我在哪里丢失了它,如果有其他方法可以不用转换成字符串,我也会很感激 public static int maharishi(int functionNum){ String num = Integer.toString(functionNum); int length = num.length(); int count = 0; int tempCount = 0; int charL
public static int maharishi(int functionNum){
String num = Integer.toString(functionNum);
int length = num.length();
int count = 0;
int tempCount = 0;
int charLetter = 0;
for(int i = 1; i < length; i++ ){
for(int j = 1; j < length; j++){
if(i==1 && j!=1 ){
if(num.charAt(i) == num.charAt(j)){
tempCount++;
if(tempCount > count){
count = tempCount;
charLetter = i;
}
}
}
}
}
char highestChar = num.charAt(charLetter);
int change = Integer.parseInt(String.valueOf(highestChar));
return change;
}
公共静态int-maharishi(int-functionNum){
字符串num=Integer.toString(functionNum);
int length=num.length();
整数计数=0;
int tempCount=0;
int charLetter=0;
for(int i=1;i计数){
计数=临时计数;
charLetter=i;
}
}
}
}
}
char highestChar=num.charAt(charLetter);
int change=Integer.parseInt(String.valueOf(highestChar));
回报变化;
}
- 中间的
条件make all失败,则仅当如果
为0时才计数,因此不需要查看其他值i
- 如果在内部循环之后进行比较,则不需要检查每个值
循环应该从内部
只读取下一个字符i+1开始
应在每一轮外部循环的tempCount
处重新初始化0
地图
:
公共静态int maharishiMaheshYogi(int functionNum){
//将数字转换为字符串
字符串num=Integer.toString(functionNum);
//创建一个映射,在其中存储每个字符计数
最终映射计数=新HashMap();
//迭代此字符串的每个字符
最终整数长度=数值长度();
for(int i=0;imaxEntry.getValue()){
maxEntry=entry;
}
}
返回Integer.parseInt(maxEntry.getKey().toString());
}
如果您不想转换为字符串,最简单的方法是使用“助手数组”来保存从0到9的每个可能数字的计数
public static int maharishi(int functionNum) {
int [] counts = new int[10];
//count all the digits in the number:
while (functionNum > 0) {
counts[functionNum % 10] += 1;
functionNum /= 10;
}
int record = -1;
int mostRepeated = -1;
//find higest most repeated digit
for (int i = 0; i < counts.length; i++) {
if (counts[i] >= record) {
record = counts[i]; //how many times this digit is in the number
mostRepeated = i; //what digit it is
}
return mostRepeated;
}
公共静态int-maharishi(int-functionNum){
int[]计数=新的int[10];
//计算数字中的所有数字:
while(functionNum>0){
计数[functionNum%10]+=1;
functionNum/=10;
}
int记录=-1;
int mostRepeated=-1;
//查找最高重复数
for(int i=0;i=记录){
record=counts[i];//这个数字是数字的多少倍
mostRepeated=i;//它是什么数字
}
返回次数最多;
}
它所做的是:
若要在不转换为字符串的情况下将数字与数字分开,只需获得除10的余数
用模和除法分隔数字的原理是一样的。嵌套循环让我感到惊讶。为什么不只计算0、1、2、.9?如何,启发我你是否知道数组?你可以有一个包含10个条目的数组,并使用每个数字作为索引来增加相应的条目….hmm,然后在数组上循环以查找最高计数……也许你的方法毕竟没那么糟糕。;-)要获得低阶数字,请使用
n%10
。然后使用n=n/10
将数字一位右移。谢谢它能起作用,我正在试着理解我所有的错误,有没有办法不将其转换为字符串就能做到这一点?@abelkibee不太好,这是一个好方法轻松访问不同元素的方法;)很好…谢谢Borois也很有用…谢谢这也很有用…非常感谢
public static int maharishi(int functionNum) {
String l = Arrays.stream(Integer.toString(functionNum).split(""))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.stream()
.max(Map.Entry.comparingByValue())
.orElseThrow()
.getKey();
return Integer.parseInt(l);
}
public static int maharishi(int functionNum) {
int [] counts = new int[10];
//count all the digits in the number:
while (functionNum > 0) {
counts[functionNum % 10] += 1;
functionNum /= 10;
}
int record = -1;
int mostRepeated = -1;
//find higest most repeated digit
for (int i = 0; i < counts.length; i++) {
if (counts[i] >= record) {
record = counts[i]; //how many times this digit is in the number
mostRepeated = i; //what digit it is
}
return mostRepeated;
}