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

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

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

import java.util.*;

// 利用replace
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        HashMap<Character, Integer> map = new HashMap<>();
        String s = in.nextLine();
        for (int i = 0; i < s.length(); i++) {
            String tep = s.replace(s.charAt(i) + "", "");
            if (s.length() == tep.length() + 1){
                System.out.println(s.charAt(i));
                return;
            }
        }
        System.out.println(-1);

    }
}

全部评论

相关推荐

04-08 10:36
已编辑
华南理工大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务