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

xiaoxiao2021-02-27  221

//题目:输入一个数组,找出第一个只出现一次的字符 //使用辅助空间 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 ' '; } }
转载请注明原文地址: https://www.6miu.com/read-11358.html

最新回复(0)