[英]Eliminating repeated characters in a string
您好,我正在尝试让这件作品正常工作,以便当输入是诸如“aaabbbccdddeef”之类的字符串时,输出是“abcdef”。 我知道那里有解决方案,但令我困扰的是这不起作用,我不明白为什么。 如果有人能帮我理解为什么这段代码不起作用,我将不胜感激。
Scanner input = new Scanner(System.in);
System.out.print("please enter the string of characters: " );
String str = input.nextLine();
char[] store = new char[str.length()];
int count =0;
for(int i=0; i<str.length();i++) {
for (int j=0; j<str.length(); j++) {
if(str.charAt(i)==store[j] ){
count+=1;//when character not stored keep count to offset store position
break;
}else {store[i-count] = str.charAt(i); count = 0;}
}
}
System.out.println(str);
System.out.print(store);
另一种方法是附加到 StringBuilder 如果它不存在
Scanner input = new Scanner(System.in);
System.out.print("please enter the string of characters: " );
String str = input.nextLine();
StringBuilder store = new StringBuilder ();
for(int i=0; i<str.length();i++) {
if (!store.toString().contains(Character.toString(str.charAt(i)))) {
store.append(str.charAt(i));
}
}
System.out.println(str);
System.out.print(store);
需要更改第二个for
循环内的逻辑。 您必须在第二个 for 循环中迭代store
而不是str
。 检查我的解决方案:
Scanner input = new Scanner(System.in);
System.out.print("please enter the string of characters: ");
String str = input.nextLine();
char[] store = new char[str.length()];
int count = 0;
boolean charInStore = false;
for (int i = 0; i < str.length(); i++) {
charInStore = false;
for (int j = 0; j < store.length; j++) {
if (str.charAt(i) == store[j]) {
charInStore = true;
break;
}
}
if (!charInStore) {
store[count] = str.charAt(i);
count++;
}
}
System.out.println(str);
System.out.println(new String(store).trim());
你真的需要利用桌面检查来更好地了解你的代码在做什么......
+------+------+--------+--------------------+-------+
| i | j | str | store | count |
+------+------+--------+--------------------+-------+
| 0 | 0 | aaabbb | [a, , , , , ] | 0 |
| 0 | 1 | aaabbb | [a, , , , , ] | 0 |
| 0 | 2 | aaabbb | [a, , , , , ] | 0 |
| 0 | 3 | aaabbb | [a, , , , , ] | 0 |
| 0 | 4 | aaabbb | [a, , , , , ] | 0 |
| 0 | 5 | aaabbb | [a, , , , , ] | 0 |
| 1 | 0 | aaabbb | [a, , , , , ] | 1 |
| --- break |
| 2 | 0 | aaabbb | [a, , , , , ] | 2 |
| --- break |
| 3 | 0 | aaabbb | [a, b, , , , ] | 0 |
| 3 | 1 | aaabbb | [a, b, , , , ] | 1 |
| --- break |
| 4 | 0 | aaabbb | [a, b, , b, , ] | 0 |
| 4 | 1 | aaabbb | [a, b, , b, , ] | 1 |
| --- break |
| 5 | 0 | aaabbb | [a, b, , b, b, ] | 0 |
| 5 | 1 | aaabbb | [a, b, , b, b, ] | 1 |
| --- break |
+------+------+--------+--------------------+-------+
核心问题是store[i - count] = str.charAt(i);
. 在您了解正在发生的事情之前,这可能并不明显。
让我们仔细看看事情开始出错的地方......
+------+------+--------+--------------------+-------+
| i | j | str | store | count |
+------+------+--------+--------------------+-------+
| --- break |
| 3 | 0 | aaabbb | [a, b, , , , ] | 0 |
| 3 | 1 | aaabbb | [a, b, , , , ] | 1 |
| --- break |
| 4 | 0 | aaabbb | [a, b, , b, , ] | 0 |
| 4 | 1 | aaabbb | [a, b, , b, , ] | 1 |
| --- break |
+------+------+--------+--------------------+-------+
好的,当i
= 4
且j
为0
str.charAt(i)
= b
store[j]
= a
count
= 0
所以, b
!= a
,所以你使用store[i - count]
,这相当于store[4 - 0]
并在那个点存储str.charAt(i)
(或b
)
事情从那里开始失控。
“基本”问题是count
在循环之间没有相关性。 无论如何,我也会质疑是否需要两个循环
这可以是一个简单的解决方案,也可以是一个非常复杂的解决方案。
我的解决方案在我看来非常简单:创建一个循环遍历字符串的整个长度然后使用 + charAt 的索引来确定字符串是否重复超过 1 创建名为 temp 的新字符串。 然后,如果是这样,请删除这些字符中的每一个,只留下一个然后打印 temp
String.indexOf('a');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.