取出一个字符串中字母出现的次数。如:字符串:"abcde%^kka27qoq" ,输出格式为: a(2)b(1)k(2)...
2016-07-24 18:25
567 查看
考虑到效率问题,鄙人发现网上应该有两种不同的方法,经过验证发现,用for循环实现比较快捷(花的时间少):
程序如下:
(1)for循环简单思路就是把源字符串转换成字符数组并挨个比较;
(2)另外一种方法是用Map集合,由于Map集合中是不允许有键重复,利用这一特点实现;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class 计算字符个数效率 {
<span style="white-space:pre"> </span>public static void main(String[] args) {
<span style="white-space:pre"> </span>String string = "abcde%^kka27qoq";
<span style="white-space:pre"> </span>System.out.println(string);
<span style="white-space:pre"> </span>long start = System.nanoTime();
<span style="white-space:pre"> </span>fun1(string);
<span style="white-space:pre"> </span>long end = System.nanoTime();
<span style="white-space:pre"> </span>System.out.println("\ncost time:" + (end - start));
<span style="white-space:pre"> </span>start = System.nanoTime();
<span style="white-space:pre"> </span>fun2(string);
<span style="white-space:pre"> </span>end = System.nanoTime();
<span style="white-space:pre"> </span>System.out.println("\ncost time:" + (end - start));
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static void fun1(String str) {
<span style="white-space:pre"> </span>char[] cs = str.toCharArray();
<span style="white-space:pre"> </span>for (int i = 0; i < cs.length; i++) {
<span style="white-space:pre"> </span>if (cs[i] == ' ') {
<span style="white-space:pre"> </span>continue;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>int count = 1;
<span style="white-space:pre"> </span>for (int j = i + 1; j < cs.length; j++) {
<span style="white-space:pre"> </span>if (cs[i] == cs[j]) {
<span style="white-space:pre"> </span>count++;
<span style="white-space:pre"> </span>cs[j] = ' ';
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>System.out.print(cs[i] + "(" + count + ")");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static void fun2(String str) {
<span style="white-space:pre"> </span>Map<Character, Integer> map = new HashMap<Character, Integer>();
<span style="white-space:pre"> </span>char[] cs = str.toCharArray();
<span style="white-space:pre"> </span>for (int i = 0; i < cs.length; i++) {
<span style="white-space:pre"> </span>Integer count = map.get(cs[i]);
<span style="white-space:pre"> </span>if (count == null) {
<span style="white-space:pre"> </span>count = 0;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>map.put(cs[i], count + 1);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>Set<Entry<Character, Integer>> entrySet = map.entrySet();
<span style="white-space:pre"> </span>for (Entry<Character, Integer> entry : entrySet) {
<span style="white-space:pre"> </span>System.out.print(entry.getKey() + "(" + entry.getValue() + ")");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
结果如下:
程序如下:
(1)for循环简单思路就是把源字符串转换成字符数组并挨个比较;
(2)另外一种方法是用Map集合,由于Map集合中是不允许有键重复,利用这一特点实现;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class 计算字符个数效率 {
<span style="white-space:pre"> </span>public static void main(String[] args) {
<span style="white-space:pre"> </span>String string = "abcde%^kka27qoq";
<span style="white-space:pre"> </span>System.out.println(string);
<span style="white-space:pre"> </span>long start = System.nanoTime();
<span style="white-space:pre"> </span>fun1(string);
<span style="white-space:pre"> </span>long end = System.nanoTime();
<span style="white-space:pre"> </span>System.out.println("\ncost time:" + (end - start));
<span style="white-space:pre"> </span>start = System.nanoTime();
<span style="white-space:pre"> </span>fun2(string);
<span style="white-space:pre"> </span>end = System.nanoTime();
<span style="white-space:pre"> </span>System.out.println("\ncost time:" + (end - start));
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static void fun1(String str) {
<span style="white-space:pre"> </span>char[] cs = str.toCharArray();
<span style="white-space:pre"> </span>for (int i = 0; i < cs.length; i++) {
<span style="white-space:pre"> </span>if (cs[i] == ' ') {
<span style="white-space:pre"> </span>continue;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>int count = 1;
<span style="white-space:pre"> </span>for (int j = i + 1; j < cs.length; j++) {
<span style="white-space:pre"> </span>if (cs[i] == cs[j]) {
<span style="white-space:pre"> </span>count++;
<span style="white-space:pre"> </span>cs[j] = ' ';
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>System.out.print(cs[i] + "(" + count + ")");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static void fun2(String str) {
<span style="white-space:pre"> </span>Map<Character, Integer> map = new HashMap<Character, Integer>();
<span style="white-space:pre"> </span>char[] cs = str.toCharArray();
<span style="white-space:pre"> </span>for (int i = 0; i < cs.length; i++) {
<span style="white-space:pre"> </span>Integer count = map.get(cs[i]);
<span style="white-space:pre"> </span>if (count == null) {
<span style="white-space:pre"> </span>count = 0;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>map.put(cs[i], count + 1);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>Set<Entry<Character, Integer>> entrySet = map.entrySet();
<span style="white-space:pre"> </span>for (Entry<Character, Integer> entry : entrySet) {
<span style="white-space:pre"> </span>System.out.print(entry.getKey() + "(" + entry.getValue() + ")");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
结果如下:
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树