用java实现,查找字符串中第一个没有重复出现的字符
来源:互联网 发布:nginx servername作用 编辑:程序博客网 时间:2024/05/16 02:40
一道笔试题,用java实现,查找字符串中第一个没有重复出现的字符。譬如“teeter”就输出“r” ,
如果是“teeterh”,还是输出“r” 。
Java Codes:1. public class TestCal{
2. public void Cal( String s ){
3. int n=0;
4. for(int i=0; i<s.length(); i++){
5. char t = s.charAt(i);
6. for(int j=0; j<s.length(); j++){
7. char tt = s.charAt(j);
8. if(t==tt)
9. n+=1;
10. }
11. if(n==1){
12. System.out.println("out result: "+t);
13. break;
14. }
15. n = 0;
16. }
17. }
18.
19. public static void main(String args[]) {
20. TestCal cal = new TestCal();
21. cal.Cal("teeter");
22. cal.Cal("teeterh");
23. cal.Cal("aaeeqerrabca");
24. cal.Cal("abc");
25. }
26. }
效率稍好的方法。
1. public static void main(String args[]) {
2. String inputStr = "teterhrecd";
3. HashMap hm = new HashMap();
4. char[] c = inputStr.toCharArray();
5. boolean isEcho;
6. //outside:
7. for (int i = 0; i < c.length; i++) {
8. if (hm.get(String.valueOf(c[i])) == null) {
9. isEcho=false;
10. //inside:
11. for (int j = i + 1; j < c.length; j++) {
12. if (c[i] == c[j]) {
13. isEcho=true;
14. hm.put(String.valueOf(c[i]), String.valueOf(c[i]));
15. break inside;
16. }
17. }
18. if(!isEcho){
19. System.out.println(c[i]);
20. break outside;
21. }
22. }
23. }
24. }
简单的方法。
1. public class Simple{
2. public static char getChar(String text){
3. char c =0xff;
4. for(int index =0;index <text.length();index ++){
5. c =text.charAt(index);
6. if(text.indexOf(c) ==text.lastIndexOf(c)) break;
7. }
8. return c;
9. }
10.
11. public static void main(String[] args){
12. System.out.println(getChar("teeter"));
13. System.out.println(getChar("teeterh"));
14. }
15. }
- 用java实现,查找字符串中第一个没有重复出现的字符
- 用java实现输出字符串中第一个出现不重复的字符
- 查找字符串中第一个出现的不重复的字符
- 字符串查找第一个不重复的字符的实现
- java实现--字符串中第一个单独出现的字符
- 查找字符串中第一个不重复的字符
- 查找字符串中第一个不重复的字符
- JS实现查找字符串中第一个不重复的字符
- 字符串第一个重复出现的字符
- 查找字符串中第一个出现一次的字符
- 查找字符串中第一个只出现一次的字符
- 查找字符串中第一个只出现一次的字符
- 查找字符串中第一个只出现一次的字符
- 查找字符串中第一个只出现一次的字符
- 查找字符串中第一个只出现一次的字符
- 查找字符串中第一个只出现一次的字符
- 给定一个字符串,查找该字符串中第一个重复出现的字符索引。要求:时间复杂度为O(n)。
- 16、JAVA 找出一个字符串中,第一个重复出现的字符
- 如何打开443端口
- 用户手册
- UVa Problem Solution: 10089 - Repackaging
- 软件需求说明书
- C#添加鼠标右击事件
- 用java实现,查找字符串中第一个没有重复出现的字符
- RAILS 学习日记 --扩展
- 大公司的php面试题http://topic.csdn.net/u/20071202/16/e53996de-4725-4dfa-bf14-c197afeed93c.html
- 软件设计本质论(Essential Design) —从链表设计说起
- 扩展CalendarExtender的应用场合
- SQL常用语句有哪些?
- 个人喜好的一些Firefox插件
- 小卡车转弯致10米长钢管扎入公交车
- Spring 基础