题目
https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof
解法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public class SO50_firstUniqChar { public static void main(String[] args) {
}
public char firstUniqChar(String s) { if (s== null || s.length() ==0) return ' '; Map<Character, Boolean> map = new HashMap<>(); char[] str = s.toCharArray(); for (char c : str) { map.put(c, !map.containsKey(c)); } for (char c : str) { if (map.get(c)) return c; } return ' '; }
public char firstUniqChar2(String s) { if (s== null || s.length() ==0) return ' '; Map<Character, Boolean> map = new LinkedHashMap<>(); char[] strs = s.toCharArray(); for (char c : strs) { map.put(c, !map.containsKey(c)); } for (Map.Entry<Character, Boolean> d : map.entrySet()) { if (d.getValue()) return d.getKey(); } return ' '; } public char firstUniqChar1(String s) { int[] count = new int[26]; for(int i = 0;i<s.length();i++){ count[s.charAt(i)-'a']++; } for(int i = 0;i<s.length();i++){ if(count[s.charAt(i)-'a']==1) return s.charAt(i); } return ' '; } }
|