取出一个字符串中字母出现的次数。如:字符串:"abcde%^kka27qoq" ,输出格式为: a(2)b(1)k(2)...
来源:互联网 发布:蛙5火箭知乎 编辑:程序博客网 时间:2024/06/06 02:05
考虑到效率问题,鄙人发现网上应该有两种不同的方法,经过验证发现,用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>}}
结果如下:
0 0
- 10、取出一个字符串中字母出现的次数。如:字符串:"abcde%^kka27qoq" ,输出格式为: a(2)b(1)k(2)...
- 取出一个字符串中字母出现的次数。如:字符串:"abcde%^kka27qoq" ,输出格式为: a(2)b(1)k(2)...
- 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为: a(2)b(1)k(2)
- 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)..
- 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)..(注意,字母出现的次数,要过滤掉数字)
- Java编程机试:获取某一字符串中(只要字母),每一个字母出现的次数输出格式为a(x)b(y)c(z).....m(n)
- 在一个字符串中找到出现指定次数的字符,根据用户输入次数,打印出现次数为该次数的字符。如输入abaccedff及1,则输出:b e d 如输入abaccedff及2,则输出:a c f
- 获取该字符串中,每一个字母出现的次数,要求打印结果是a(1)b(2)...
- 一个字符串中字母出现的次数
- 编程. 已知字符串:"this is a test of java". 按要求执行以下操作: (1) 统计该字符串中字母s出现的次数 (2) 取出子字符串"test" (3) 用多种方式将本字
- 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。 比如字符串“abacacde”过滤结果为“abcde”。
- 一个字符串中可能包含a~z中的多个字符,如有重复,求出现次数最多的那个字母及次数,如有多个重复最多的则都输出。
- 4.写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能: 1)输出字符串的长度。 2)输出字符串中第一个出现字母a的位置。 3)在字符串的第3个字符后面插入子串“hello”,输出新字符串。 4)将字符串“hello”替换为“me”,输出
- java面试题 "aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)
- "bcdabcdeaababca",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)
- 求字符串中字母出现的次数并排序输出
- 统计字符串中出现次数最多的字母并输出
- java Map输出字符串中字母出现的次数
- 自学360之滚动的TextView
- HDU 5724 博弈,SG函数
- 【 Codeforces 615A】A. Bulbs
- Android之基于位置的服务
- WPF编程宝典读书笔记——第10章 资源(一)
- 取出一个字符串中字母出现的次数。如:字符串:"abcde%^kka27qoq" ,输出格式为: a(2)b(1)k(2)...
- Alice and Bob
- POJ 2406 Power Strings (kmp求循环节)
- Android中的事件分发和处理
- Android 补间动画
- SharedPreferences 监听变化Listener 的onSharedPreferenceChanged方法没有调用
- Iframe实现Ajax文件上传Servlet响应上传结果
- C#:网络通信(TCP)
- 第一章.对象导论之方法的重写和重载