2016 360笔试题------找第一个仅仅出现一次的字符

import java.util.List;
import java.util.Scanner;


public class Main {


public static void main(String[] args) throws IOException {


InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String buffer = null;

int number = Integer.parseInt(br.readLine());
while(number-- > 0){
buffer=br.readLine();
char ch = find(buffer);
System.out.println(ch);
}
br.close();
isr.close();
}
//開始寻找第一个仅仅出现一次的字符
public static char find(String buffer){
LinkedHashMap<Character, Integer> hm = new LinkedHashMap<Character, Integer>();
char []str = buffer.toCharArray();
for(int i = 0; i<str.length; i++){
char key = str[i];
if(hm.containsKey(key)){
hm.put(key, 2);
}else{
hm.put(key, 1);
}
}
//開始遍历查询
Iterator<Entry<Character, Integer>> iterator = hm.entrySet().iterator();
while(iterator.hasNext()){
Entry<Character, Integer> e = iterator.next();
int count = e.getValue();
if(count == 1){
return e.getKey();
}
}
return ' ';
}

}

posted @ 2017-05-17 09:41  lytwajue  阅读(138)  评论(0编辑  收藏  举报