Java:打印字符串中的唯一字符
我正在编写一个程序,将打印字符串中的唯一字符(通过扫描仪输入)。我创建了一个方法,试图实现这一点,但我不断得到不重复的字符,而不是字符串特有的字符。我只想要独一无二的字母
这是我的代码:
import java.util.Scanner;
public class Sameness{
public static void main (String[]args){
Scanner kb = new Scanner (System.in);
String word = "";
System.out.println("Enter a word: ");
word = kb.nextLine();
uniqueCharacters(word);
}
public static void uniqueCharacters(String test){
String temp = "";
for (int i = 0; i < test.length(); i++){
if (temp.indexOf(test.charAt(i)) == - 1){
temp = temp + test.charAt(i);
}
}
System.out.println(temp + " ");
}
}
下面是上面代码的示例输出:
Enter a word:
nreena
nrea
预期的输出是:ra
# 1 楼答案
应用接吻原则怎么样:
# 2 楼答案
这是一个面试问题。找出字符串的所有唯一字符。 以下是完整的解决方案。代码本身是不言自明的
在Python中:
# 3 楼答案
根据所需的输出,您必须替换一个最初已添加的字符,然后再添加一个重复的字符,因此: