测试用例没通过:
package imut.lanqiaobei.text01;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Map map = new HashMap<>();
Scanner sc = new Scanner(System.in);
String s = sc.next();
int length = s.length();
for (int i = 0; i < length; i++) {
if (map.containsKey(s.charAt(i))) {
int val = map.get(s.charAt(i)) + 1;
map.put(s.charAt(i), val);
} else {
map.put(s.charAt(i), 1);
}
}
int max = 0;
String result = "";
for (int i = 'A'; i <= 'Z'; i++) {
if (map.containsKey((char) i)) {
if (max < map.get((char) i)) {
result = "" + (char) i;
max = map.get((char) i);
}else if (max == map.get((char) i)) {
result += "" + (char) i;
}
}
}
System.out.println(result);
}
}
感觉所以的情况都可以包涵,但是为什么最后显示答案错误?
在代码开头,应该使用 import java.util.*; 导入 Map 和 Scanner 类。在读取字符串之前,应该使用 sc.nextLine() 将之前的输入缓冲区中的内容读取掉,否则会读到回车符,导致下一行代码直接结束。在计算字符出现次数的循环中,可以使用 map.merge() 方法代替 if-else 语句,简化代码。在遍历字母时,应该使用字母的 ASCII 码值 'A' 到 'Z',而不是直接使用字符。
以下是修改后的代码:
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<Character, Integer> map = new HashMap<>();
Scanner sc = new Scanner(System.in);
sc.nextLine(); // 读取之前的输入缓冲区中的内容
String s = sc.nextLine(); // 读取字符串
int length = s.length();
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
map.merge(c, 1, Integer::sum); // 计算字符出现次数
}
int max = 0;
String result = "";
for (int i = 'A'; i <= 'Z'; i++) {
char c = (char) i;
if (map.containsKey(c)) {
int count = map.get(c);
if (count > max) {
result = String.valueOf(c);
max = count;
} else if (count == max) {
result += c;
}
}
}
System.out.println(result);
}
}
package JAVA_Project_01_04;//创建一个包
/*
提供两种思路解决去掉重复数字:第一种是增加一个数组,但是长度无法确定,记录没有重复的值;
第二种是增加一个数组,用于记录原数组中存在的数,再增加一个数组可以确定数组的长度,用于存放原数组的值。
*/
public class TextDelRepeat {//操作去掉数组中重复数字的类
public static int[]changeMethodOne(int src[]){//方法一 去掉重复数字
int length=src.length;//获得传入数组的长度
int[] taget =new int[length];//声明一个数组,长度为传入数组的长度
int index = 0;
taget[0]=src[0];//设置数组的初始值
for (int i = 1; i < length; i++) {//循环遍历传入数组
if (taget[index]!=src[i]){//遍历数组与初始值进行比较
index++;//等价于index=index+1
taget[index]=src[i];//元素赋值
}
}
return taget;//返回结果数组
}
public static int[]changeMethodTwo(int src[]){//方法二 去掉重复数字
int length =src.length;//获得传入数组的长度
int[] tagetIndex=new int[length];//声明一个数组,长度为传入数组的长度
int tagetLength = length;
for (int i = 0; i < length; i++) {
tagetIndex[i]=0;//初始化tagetIndex
}
for (int j = 1; j < length; j++) {//记录重复的值
if (src[j]==src[j-1]){
tagetIndex[j]=1;
tagetLength--;
}
}
int[] target =new int[tagetLength];
int index=0;
for (int k = 0; k < length; k++) {//循环将数组赋值
if (tagetIndex[k]==0){//数组元素等于1是存放重复数字
target[index++]=src[k];
}
}
return target;
}
public static void main(String[] args) {
int[] a={1,1,1,2,3,4,5,6,9,9,12,53};//声明数组并初始化
int[] b=changeMethodOne(a);//调用第一种方法去掉重复数字
System.out.println("第一种方法去掉数组中重复的数字结果:");
for (int i = 0; i < b.length; i++) {//将返回的数组输出
System.out.println(b[i]+" ");
}
System.out.println();
System.out.println("第二种方法去掉数组中重复数字结果:");
int[] c=changeMethodTwo(a);//调用第二种方法去掉重复数字
for (int i = 0; i < c.length; i++) {//将返回的数组输出
System.out.println(c[i]+" ");
}
}
}
/*
在changeMethodone()方法中,声明一个与传入数组长度相等的数组,根据循环判断是否有重复的数字,并将不重复
的数字放入声明的数组中,如果传入数组中存在重复的数字,那么这个数组的长度应会小于传入数组的长度,这样也就造成了数组长度的浪费。
在ChangeMothodTwo()方法中,也声明一个与传入数组长度相等的数组tagetIndex,值初始化全为0.循环进行判断重复数字的个数,如果存在重复数字则
数组tagetIndex元素赋值为1.然后声明一个数组target,长度为(tagetIndex.length-重复数字个数)。当tagetIndex数组中元素为0的,
将数组src元素下标等于tagetIndex元素中不为0的下标的元素赋值给target.
*/