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

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

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        Character ans = null;
        int pivot = 0;
        for(int i = 0; i < str.length(); i++){
            if(str.indexOf(str.charAt(i))==str.lastIndexOf(str.charAt(i))){
                ans = str.charAt(i);
                pivot = 1;
                break;
            }
        }
        
        if(pivot == 0){
            System.out.println(-1);
        }
        else{
            System.out.println(ans);
        }
    }
}
1.字符第一次出现的位置和最后一次出现的位置
str.indexOf(str.charAt(i))==str.lastIndexOf(str.charAt(i)
全部评论

相关推荐

lllllkin:感觉可以精简到一页简历,有些排版感觉不是必须的。 时间线越早的,你自己越熟悉的放前面。描述可以更精简些,一些问题解决感觉可以不用写具体技术栈,卖个关子,等面试官问。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务