// 输入 字符串的第一个只出现一次的字符
// 输入一个非自发字符串
// 输出第一个只出现一次额字符,如果不存在输出-1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str=sc.next();
int n=str.length();
for (int i=0;i<n;i++){
char ch=str.charAt(i);
String s=ch+"";
if ((n-str.replaceAll(s,"").length())==1){
System.out.println(str.charAt(i));
break;
}
}
}
}
}
本文介绍了一种使用Java程序来查找并返回输入字符串中首次出现的唯一字符的方法。通过遍历字符串和利用正则表达式,算法能够高效地识别出首个不重复的字符,如果不存在这样的字符,则返回-1。
981

被折叠的 条评论
为什么被折叠?



