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

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

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        String a;
        try {
            a = r.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        char[] chs = a.toCharArray();
        int l = chs.length, i = 0, tmp = 0;
        int[] cnt = new int[l];
        int[] word = new int[128];
        while (i < l) {
            tmp = chs[i];
            word[tmp]++;
            i++;
        }
        i = 0;
        while (i < l) {
            tmp = chs[i];
            cnt[i] = word[tmp];
            i++;
        }
        i = 0;
        while (i < l) {
            if (cnt[i] == 1) break;
            i++;
        }
        System.out.print(i < l ? chs[i] : "-1");
    }
}

全部评论

相关推荐

FieldMatching:看成了猪头顾问,不好意思
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务