题解 | #找出字符串中第一个只出现一次的字符#

找出字符串中第一个只出现一次的字符

https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4

import java.util.*;

// 老八秘制小汉堡
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String next = scanner.next();
            String[] split = next.split("");
            LinkedHashMap<String, Integer> hm = new LinkedHashMap<>();
            for (int i = 0; i < split.length; i++) {
                if (hm.get(split[i])==null){
                    hm.put(split[i],1);
                }else {
                    hm.put(split[i],hm.get(split[i])+1 );
                }
            }
            boolean p=false;
            for(String key:hm.keySet()){
                if (hm.get(key)==1){
                    System.out.println(key);
                    p=true;
                    break;
                }
            }
            if (!p){
                System.out.println("-1");
            }
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务