LeetCode-面试题50. 第一个只出现一次的字符

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;
	}
发布了137 篇原创文章 · 获赞 2 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/m0_37302219/article/details/104962452