题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
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)
