链接:https://www.nowcoder.com/questionTerminal/e896d0f82f1246a3aa7b232ce38029d4
来源:牛客网
找出字符串中第一个只出现一次的字符
输入描述:
输入几个非空字符串
输出描述:
输出第一个只出现一次的字符,如果不存在输出-1
示例1
输入
asdfasdfo aabb
输出
o -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.nextLine();
int N = str.length();
boolean flag = true;
boolean flag1 = true;
if (N==1){
System.out.println(str.charAt(0));
}else {
for (int i = 0; i < str.length() - 1; i++) {
for (int j = 0; j < str.length(); j++) {
if (str.charAt(i) != str.charAt(j)) {
} else if (j == i) {
} else {
flag = false;
}
}
if (flag) {
flag1 = false;
System.out.println(str.charAt(i));
break;
}
flag = true;
}
if (flag1) {
System.out.println(-1);
}
}
}
}
}