解题思路:
注意事项:
参考代码:
import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Scanner; public class 简单的字符串 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int i = 0; i < n; i++) { String s = scanner.next(); String temp = ""; char[] chars = s.toCharArray(); int length = chars.length;//提前进行长度的计算,简化计算量 LinkedHashSet set = new LinkedHashSet();//利用set集合的特性,使其存入字符串的单个字符。如:ABBCCC 存入 A B C用于后面的拼接 for (int j = 0; j < length; j++) { set.add(chars[j]); } //为了后面的取值方便,改用list集合 Iterator iterator = set.iterator();//迭代器 ArrayList ints = new ArrayList<>(); while (iterator.hasNext()) { ints.add((char)iterator.next()); } //计算每单个字符的个数 ArrayList list = new ArrayList<>(); for (int j = 0; j <ints.size(); j++) { int cnt=0; for (int k = 0; k < length; k++) { if (chars[k] == ints.get(j)) { cnt++; } } list.add(cnt); } //将单个字符和它的个数进行拼接 for (int j = 0; j <list.size() ; j++) { if (list.get(j) == 1) { temp+=ints.get(j); }else temp+=String.valueOf(list.get(j))+ints.get(j); } System.out.println(temp); } } }
0.0分
1 人评分