计算字符串中的唯一字符
Counting unique characters in a string
所以我一直在尝试编写一个代码来计算字符串中的单词数,这非常简单。当我试图让它计算字符串中唯一字符的数量时,我 运行 遇到了问题。该程序编译并运行它不显示唯一字符的数量。添加一个 System.out.println(countOfUniqueChars); return 以下无效。
代码如下:
public class Uniquechar{
public static void main(String[] args) {
String s = "Jag vet inte vad jag heter idag";
String[] parts = s.split(" ");
int wordcount = parts.length;
System.out.println("The number of words is" + wordcount);
countUniqueCharacters(s);
}
public static int countUniqueCharacters(String s) {
String lowerCase = s.toLowerCase();
char characters[] = lowerCase.toCharArray();
int countOfUniqueChars = s.length();
for (int i = 0; i < characters.length; i++) {
if (i != lowerCase.indexOf(characters[i])) {
countOfUniqueChars--;
}
}
return countOfUniqueChars;
}
只需打印方法调用,它就会打印结果。
System.out.println(countUniqueCharacters(s));
Adding a System.out.println(countOfUniqueChars); below return doesn't work.
不行。因为 return 语句之后的代码是不可访问的。也许你可以在 return
.
之前完成
System.out.println(countOfUniqueChars);
return countOfUniqueChars;
你可以在main方法中做System.out.println(countUniqueCharacters(s));
,输出你的方法的return值。在 return 之后,您无法添加更多代码。我给你做了,结果是12
,看来你的算法也有问题。
int uniqeCharsCount = countUniqueCharacters(s);
System.out.println("The number of uniqe chars is " + uniqeCharsCount);
输出:12
你的算法:
实际上你正在检查每个字符,如果这个字符在字符串 before
中出现一次。但是您还应该检查 char 是否在字符串 after
当前索引中的任何位置。如果将 if 条件更改为 if (i != lowerCase.indexOf(characters[i]) || i != lowerCase.lastIndexOf(characters[i]))
,则可以修复它
固定版本的输出:3 (n, h, r)
试试这个:
s = s.replace(" ", ""); // If you don't want to count space
char[] chars = s.toCharArray();
Set<Character> uniqueChars = new HashSet<>();
for (char c : chars) {
uniqueChars.add(c);
}
System.out.println(c.size());
我建议使用 Set
来仅保留唯一值,然后计算其大小,而不是迭代:
public static int countUniqueCharacters(String s) {
String lowerCase = s.toLowerCase();
char characters[] = lowerCase.toCharArray();
Set<Character> uniques = new HashSet<Character>();
for (char c: characters) {
uniques.add(c);
}
return uniques.size();
}
if (i != lowerCase.indexOf(characters[i])) {
countOfUniqueChars--;
}
这是错误的。您的 lowerCase 字符串是小写的,因此 characters[i] 中的任何大写字母在 lowerCase 中的索引都是 -1(将被计算为非唯一字符)。您可以使用 indexOf(lowerCase.charAt(i));
解决此问题
计算字符数的一个好方法是消除重复。想法是获取第一个字符,然后找到下一个出现的字符并替换为空,一旦这样做,您就可以计算唯一字符。
public static int countUniqueCharacters(String s) {
String lowerCase = s.toLowerCase();
///Get the first char of lowerCase
String firstChar = lowerCase.substring(0,1);
//Take off the first char
String subS = lowerCase.substring(1);
///replace all chars equals to first char
String replacedSubS = subS.replace(firstChar, "");
/// Now, call method again to calculate size
/// of the substring with first char
// replaced by blank char
return 1+countUniqueCharacters(replacedSubS);
}
这个方法对我有用,你看看。您可以分两行进行,但我认为最好在此处详细说明。
Adding a System.out.println(countOfUniqueChars);
below return
doesn't work.
这是预期的行为,因为 return
意味着控制流将从方法 returned 到调用此方法的地方。这意味着 return
之后的代码将不会被执行,所以在
这样的情况下
return countOfUniqueChars;
System.out.println(countOfUniqueChars);
System.out.println(countOfUniqueChars);
将是 死代码 .
你可以在 return 之前尝试打印值,就像
System.out.println(countOfUniqueChars);
return countOfUniqueChars;
或者简单地在 main 方法中打印 returned 值,例如
int count = countUniqueCharacters(s);
System.out.println(count);
或者使用这个单行
System.out.println(countUniqueCharacters(s));
顺便说一句,因为 Java 8 你的代码看起来像
s.toLowerCase().chars().distinct().summaryStatistics().getCount()
或者如果您想跳过空格,您可以添加
s.toLowerCase().replace(" ","").chars().distinct().summaryStatistics().getCount()
public static int countUniqueCharacters(String s) {
char [] input=s.toCharArray();
Set<Character> charset=new HashSet<>();
for (int i = 0; i < input.length; i++) {
charset.add(input[i]);
}
return charset.size();
}
所以我一直在尝试编写一个代码来计算字符串中的单词数,这非常简单。当我试图让它计算字符串中唯一字符的数量时,我 运行 遇到了问题。该程序编译并运行它不显示唯一字符的数量。添加一个 System.out.println(countOfUniqueChars); return 以下无效。
代码如下:
public class Uniquechar{
public static void main(String[] args) {
String s = "Jag vet inte vad jag heter idag";
String[] parts = s.split(" ");
int wordcount = parts.length;
System.out.println("The number of words is" + wordcount);
countUniqueCharacters(s);
}
public static int countUniqueCharacters(String s) {
String lowerCase = s.toLowerCase();
char characters[] = lowerCase.toCharArray();
int countOfUniqueChars = s.length();
for (int i = 0; i < characters.length; i++) {
if (i != lowerCase.indexOf(characters[i])) {
countOfUniqueChars--;
}
}
return countOfUniqueChars;
}
只需打印方法调用,它就会打印结果。
System.out.println(countUniqueCharacters(s));
Adding a System.out.println(countOfUniqueChars); below return doesn't work.
不行。因为 return 语句之后的代码是不可访问的。也许你可以在 return
.
System.out.println(countOfUniqueChars);
return countOfUniqueChars;
你可以在main方法中做System.out.println(countUniqueCharacters(s));
,输出你的方法的return值。在 return 之后,您无法添加更多代码。我给你做了,结果是12
,看来你的算法也有问题。
int uniqeCharsCount = countUniqueCharacters(s);
System.out.println("The number of uniqe chars is " + uniqeCharsCount);
输出:12
你的算法:
实际上你正在检查每个字符,如果这个字符在字符串 before
中出现一次。但是您还应该检查 char 是否在字符串 after
当前索引中的任何位置。如果将 if 条件更改为 if (i != lowerCase.indexOf(characters[i]) || i != lowerCase.lastIndexOf(characters[i]))
固定版本的输出:3 (n, h, r)
试试这个:
s = s.replace(" ", ""); // If you don't want to count space
char[] chars = s.toCharArray();
Set<Character> uniqueChars = new HashSet<>();
for (char c : chars) {
uniqueChars.add(c);
}
System.out.println(c.size());
我建议使用 Set
来仅保留唯一值,然后计算其大小,而不是迭代:
public static int countUniqueCharacters(String s) {
String lowerCase = s.toLowerCase();
char characters[] = lowerCase.toCharArray();
Set<Character> uniques = new HashSet<Character>();
for (char c: characters) {
uniques.add(c);
}
return uniques.size();
}
if (i != lowerCase.indexOf(characters[i])) {
countOfUniqueChars--;
}
这是错误的。您的 lowerCase 字符串是小写的,因此 characters[i] 中的任何大写字母在 lowerCase 中的索引都是 -1(将被计算为非唯一字符)。您可以使用 indexOf(lowerCase.charAt(i));
解决此问题计算字符数的一个好方法是消除重复。想法是获取第一个字符,然后找到下一个出现的字符并替换为空,一旦这样做,您就可以计算唯一字符。
public static int countUniqueCharacters(String s) {
String lowerCase = s.toLowerCase();
///Get the first char of lowerCase
String firstChar = lowerCase.substring(0,1);
//Take off the first char
String subS = lowerCase.substring(1);
///replace all chars equals to first char
String replacedSubS = subS.replace(firstChar, "");
/// Now, call method again to calculate size
/// of the substring with first char
// replaced by blank char
return 1+countUniqueCharacters(replacedSubS);
}
这个方法对我有用,你看看。您可以分两行进行,但我认为最好在此处详细说明。
Adding a
System.out.println(countOfUniqueChars);
belowreturn
doesn't work.
这是预期的行为,因为 return
意味着控制流将从方法 returned 到调用此方法的地方。这意味着 return
之后的代码将不会被执行,所以在
return countOfUniqueChars;
System.out.println(countOfUniqueChars);
System.out.println(countOfUniqueChars);
将是 死代码 .
你可以在 return 之前尝试打印值,就像
System.out.println(countOfUniqueChars);
return countOfUniqueChars;
或者简单地在 main 方法中打印 returned 值,例如
int count = countUniqueCharacters(s);
System.out.println(count);
或者使用这个单行
System.out.println(countUniqueCharacters(s));
顺便说一句,因为 Java 8 你的代码看起来像
s.toLowerCase().chars().distinct().summaryStatistics().getCount()
或者如果您想跳过空格,您可以添加
s.toLowerCase().replace(" ","").chars().distinct().summaryStatistics().getCount()
public static int countUniqueCharacters(String s) {
char [] input=s.toCharArray();
Set<Character> charset=new HashSet<>();
for (int i = 0; i < input.length; i++) {
charset.add(input[i]);
}
return charset.size();
}