package edu.LeetCode.二;
import java.util.HashMap;
import java.util.Map;
public class 记录字符出现次数 {
// public int longestPalindrome(String s){
public static void main(String[] args) {
String str = "abccccdd";
char[] ch = str.toCharArray();//将字符串str转化成字符数组;
Map map = new HashMap();
for (int i = 0; i < ch.length; i++) {
Integer count = (Integer) map.get(String.valueOf(ch[i]));//用count记录ch[i]在map集合中出现的次数
if (count == null) {
count = 1;
map.put(String.valueOf(ch[i]), count);
} else {
count++;
map.put(String.valueOf(ch[i]), count);
}
}
for (Object key : map.keySet()) {//map.keySet()获取所有的key值
System.out.println(map.get(key) + "个" + key);
}
}
}
记录字符出现的次数
最新推荐文章于 2024-03-12 20:28:58 发布