您的位置:首页 > Web前端

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

2017-05-05 08:17 274 查看
//题目:输入一个数组,找出第一个只出现一次的字符
//使用辅助空间
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 ' ';
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: