题4

来源:互联网 发布:nginx 代理静态页面 编辑:程序博客网 时间:2024/06/05 17:16
import java.util.HashMap;import java.util.Map;import java.util.Scanner;/** * Created by Administrator on 2017/9/22. * 在一个字符串中找到第一个只出现一次的字符。 */public class Main8 {    public static void main(String[] args) {        Scanner sc=new Scanner(System.in);        while(sc.hasNext()){            String s=sc.next();            HashMap<Character,Integer> mapa=new HashMap<>();            HashMap<Character,Integer> mapb=new HashMap<>();            char[] c=s.toCharArray();            for (int i = 0; i <c.length; i++) {                if(!mapa.containsKey(c[i])){                    mapa.put(c[i],1);                }else mapa.put(c[i],mapa.get(c[i])+1);                mapb.put(c[i],i);            }            int index=c.length-1;            for(Map.Entry<Character,Integer> e:mapa.entrySet()){                if(e.getValue()==1){                    if(index>mapb.get(e.getKey())){                        index=mapb.get(e.getKey());                    }                }            }            System.out.println(c[index]);        }    }}
原创粉丝点击