剑指offer 35. 第一次只出现一次的字符

来源:互联网 发布:移动硬盘修理软件 编辑:程序博客网 时间:2024/06/08 10:26
//题目:输入一个数组,找出第一个只出现一次的字符//使用辅助空间public class Main {public static void main(String[] args) throws Exception {System.out.println(findFirstChar("abaccdeff"));}public static char findFirstChar(String str){if(str == null){return ' ';}HashMap<Character, Integer> m = new HashMap<Character, Integer>();for(int i = 0;i<str.length();i++){char temp = str.charAt(i);int count = 1;if(m.containsKey(temp)){count = m.get(temp)+1;}m.put(temp, count);}for(int i = 0;i<str.length();i++){char temp = str.charAt(i);if(m.get(temp) == 1){return temp;}}System.out.println("not found");return ' ';}}

0 0
原创粉丝点击