给定一个字符串,请写一段代码找出这个字符串中首先出现三次的那个英文字符(需要区分大小写)。
数据范围:字符串长度满足
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String ss = sc.nextLine(); int[] arr = new int [150]; int n = 0; for(int i=65;i<=90;i++){ arr[i] = 1; } for(int i=97;i<=122;i++){ arr[i] = 1; } int index=0; for(int j =0;j<ss.length();j++){ if(index!=0){ break; } arr[(int)ss.charAt(j)]+=1; for(int i=65;i<=122;i++){ if(arr[i] == 4){ index = j; System.out.println(ss.charAt(index)); break; } } } } } 纯计数2333
import java.util.Scanner; import java.util.Map; import java.util.TreeMap; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ char[] ch = sc.nextLine().toCharArray(); Map<Character,Integer> map = new TreeMap<>(); for(int i=0;i<ch.length;i++){ if((ch[i] >='a' && ch[i]<='z') || (ch[i] >='A' && ch[i] <='Z')){ if(map.containsKey(ch[i])){ map.put(ch[i],map.get(ch[i])+1); if(map.get(ch[i]) == 3){ System.out.println(ch[i]); break; } }else{ map.put(ch[i],1); } } } } } }
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String string = in.nextLine(); string = string.replace(" ","").replaceAll("[^a-zA-Z0-9]",""); String[] strings = string.split(""); Map<String,Integer> map = new HashMap(); for (String s : strings) { if (!map.containsKey(s)) { map.put(s, 1); } else { map.put(s, map.get(s) + 1); if (map.containsValue(3)) { System.out.println(s); return; } } } } }
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); while (in.hasNext()) { String string = in.nextLine(); System.out.println(threeTime(string)); } } public static char threeTime(String str) { String string = str; //除了a-zA-Z都替换为空,使得字符串只有英文字符 string = string.replaceAll("[^a-zA-Z]", ""); HashMap<Character, Integer> map = new HashMap<>(); for (int i = 0; i < string.length(); i++) { char c = string.charAt(i); if (map.containsKey(c)) { int value = map.get(c); if (value - 2 == 0) { return c; } else { map.put(c, ++value); } } else { map.put(c, 1); } //System.out.println(c + "===" + map.get(c)); } return '#'; } }
import java.util.Scanner;
public class Main{ public static void main(String []args){ Scanner sc=new Scanner(System.in); String str=sc.nextLine().replace(" ",""); int []s=new int[127]; for(int i=0;i<str.length();i++){ s[str.charAt(i)]++; if(s[str.charAt(i)]==3&&(str.charAt(i)>='a'&&str.charAt(i)<='z'||str.charAt(i)>='A'&&str.charAt(i)<='Z')){ char c=str.charAt(i); System.out.println(c); break;} } } }
因为要区分大小写,26个大写,26个小写字母,因此维护一个大小为52的数组,来保存该字母出现的次数,遍历循环整个字符串,统计每个字符出现的次数,当出现次数为3时则找到了该字符,并跳出循环。
import java.util.Scanner;
public class CountChar {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str = sc.nextLine().trim();
char[] strs = str.toCharArray();
int[] charCount = new int[52];
for(int i=0; i<strs.length; i++) {
if(strs[i] != ' ') {
if(strs[i]>='A' && strs[i]<='Z') {
charCount[strs[i] - 'A'] ++;
if(charCount[strs[i] - 'A']==3) {
System.out.println(strs[i]);
break;
}
}else if(strs[i]>='a' && strs[i]<='z') {
charCount[strs[i] - 'a' + 26] ++;
if(charCount[strs[i] - 'a' + 26]==3) {
System.out.println(strs[i]);
break;
}
}
}
}
}
}
import java.util.LinkedHashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String s = in.nextLine();
count(s);
}
}
private static void count(String s) {
LinkedHashMap map = new LinkedHashMap();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (map.containsKey(c)) {
if (map.get(c) == 2 && Character.toUpperCase(c) >= 'A' && Character.toUpperCase(c) <= 'Z') {
System.out.println(c);
break;
}
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine();
System.out.println(search(line));
}
private static char search(String str) {
int[] counts = new int[128];
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c >= 'A' && c <= 'z') {
int index = c - 'A';
counts[index] += 1;
if (counts[index] == 3) {
return c;
}
}
}
return ' ';
}
}
//看清题目:字符串包括字母、数字等,但统计的是字母 import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { String string = in.nextLine(); int[] chs = new int[256]; for (int i = 0; i < string.length(); i++) { if (Character.isAlphabetic(string.charAt(i))) { chs[string.charAt(i)]++; if (chs[string.charAt(i)] == 3) { System.out.println(string.charAt(i)); break; } } } } } }
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String str = scanner.nextLine(); Map<Character, Integer> properties = new HashMap<>(); int countc; for (int i = 0; i < str.length(); i++) { if (properties.containsKey(str.charAt(i))) { countc = properties.get(str.charAt(i)) + 1; properties.put(str.charAt(i), countc); if (countc == 3 && ((str.charAt(i) >= 65 && str.charAt(i) <= 90) || (str.charAt(i) >= 97 && str.charAt(i) <= 122))) { System.out.println(str.charAt(i)); break; } } else { properties.put(str.charAt(i), 1); } } } } }这个题感觉最重要的就是要理解题意,我理解错了两次,第一个注意点就是输入的字符串不只是英文字母,还可能是别的字符,第二个注意点就是要输出的是第一次出现三次的英文字母,而不是任意字符。理解题意很重要,其次就是数据结构,我用的是一个Map结构,把出现的字符,以及这个字符的出现次数存了起来,出现三次的英文字母后就结束循环。
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { char[] arr = sc.nextLine().replaceAll("[^a-zA-Z]", "").toCharArray(); Map<Character, Integer> map = new HashMap<>(); for (int i = 0; i < arr.length; i ++) { map.put(arr[i], map.containsKey(arr[i]) ? map.get(arr[i]) + 1 : 1); if(map.get(arr[i]) == 3) { System.out.println(arr[i]); break; } } } } }
import java.util.HashMap;
import java.util.Scanner;
// 哪里不对求指教
/**
@desc
*/
public class ThirdWord {
public static void main(String[] args) {
//System.out.println(">".toLowerCase().equals(">"));
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
sc.close();
str = str.replaceAll(" +", "");
//System.out.println(str);
char[] chs = str.toCharArray();
HashMap<String,Integer> map = new HashMap<>();
for(char c :chs) {
if(map.get((c+"").toLowerCase())==null) {
map.put((c+"").toLowerCase(), 1);
}else {
map.put((c+"").toLowerCase(), map.get((c+"").toLowerCase())+1);
}
if(map.get((c+"").toLowerCase()) == 3) {
System.out.println(c);
break;
}
}
}
}
import java.util.*; public class Main{ public static void main(String[] atgs){ Scanner sc = new Scanner(System.in); int[] d = new int[256]; String s = sc.nextLine(); char[] a = s.toCharArray(); for(char c:a){ if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){ d[c]++; if(d[c]==3){ System.out.print(c); return; } } } } }