LeetCode- face questions 50. The first character appears only once

 

public char firstUniqChar(String s) {
		char res = ' ';
		if(s.isEmpty()) return res;
		int n=s.length();
		Map<Character, Integer> map=new HashMap<Character, Integer>();
		char c;
		int count=0;
		for(int i=0;i<n;i++) {
			c=s.charAt(i);
			if(map.containsKey(c)) {
				count=map.get(c)+1;
				map.put(c, count);
			}
			else {
				map.put(c, 1);
			}
		}
		for(int i=0;i<n;i++) {
			c=s.charAt(i);
			count=map.get(c);
			if(count==1) return c;
		}
		return res;
	}

 

Published 137 original articles · won praise 2 · views 20000 +

Guess you like

Origin blog.csdn.net/m0_37302219/article/details/104962452