如何在每次迭代时删除连续字符.. 以下是详细解释问题的屏幕截图
MySolution
最初我检查了是否有任何连续字符。 如果是,则删除所有连续字符,当没有连续字符时,将剩余字符添加到另一个字符串。
如果没有连续字符只是增加它。
public static void print(){
String s1="aabcccdee"; I have taken a sample test case
String s2="";
for(int i=0;i<s1.length();){
if(s1.charAt(i)==s1.charAt(i+1)){
while(s1.charAt(i)==s1.charAt(i+1)){
i++;
}
for(int j=i+1;j<s1.length();j++){
s2=s2+s1.charAt(j);
}
s1=s2;
}
else
i++;
}
System.out.println(s1);
}
显示输出
An infinite Loop
给定样本的预期输出
bd
任何人都可以指导我如何纠正吗?
答案 0 :(得分:1)
您只需将String::replaceFirts
与此正则表达式(.)\1+
一起使用,这意味着匹配任何字符(.)
后面跟着自己\1
一个或多个时间+
为空。
如果您想首先替换首先必须检查输入,如果每次迭代后仍然包含多个连续字符,在这种情况下,您可以使用Pattern
和Matcher
,如下所示:
String[] strings = {"aabcccdee", "abbabba", "abbd "};
for (String str : strings) {
Pattern pattern = Pattern.compile("([a-z])\\1");
// While the input contain more than one consecutive char make a replace
while (pattern.matcher(str).find()) {
// Note : use replaceFirst instead of replaceAll
str = str.replaceFirst("(.)\\1+", "");
}
System.out.println(str);
}
输出
aabcccdee -> bd
abbabba -> a
abbd -> ad
答案 1 :(得分:1)
我误解了这个问题。目的是在每次替换后删除连续的字符。以下代码就是这样做的。
private static String removeDoubles(String str) {
int s = -1;
for (int i = 1; i < str.length(); i++) {
// If the current character is the same as the previous one,
// remember its start position, but only if it is not set yet
// (its value is -1)
if (str.charAt(i) == str.charAt(i - 1)) {
if (s == -1) {
s = i - 1;
}
}
else if (s != -1) {
// If the current char is not equal to the previous one,
// we have found our end position. Cut the characters away
// from the string.
str = str.substring(0, s) + str.substring(i);
// Reset i. Notice that we don't have to loop from 0 on,
// instead we can start from our last replacement position.
i = s - 1;
// Finally reset our start position
s = -1;
}
}
if (s != -1) {
// Check the last portion
str = str.substring(0, s);
}
return str;
}
请注意,这几乎比YCF_L的答案快10倍。
你几乎就在那里,但你不必使用多个for循环。你只需要一个循环,因为是否从字符串中删除字符只取决于后续字符;我们不需要算什么。
试试这个:
private static String removeDoubles(String s) {
boolean rem = false;
String n = "";
for (int i = 0; i < s.length() - 1; i++) {
// First, if the current char equals the next char, don't add the
// character to the new string and set 'rem' to true, which is used
// to remove the last character of the sequence of the same
// characters.
if (s.charAt(i) == s.charAt(i + 1)) {
rem = true;
}
// If this is the last character of a sequence of 'doubles', then
// reset 'rem' to false.
else if (rem) {
rem = false;
}
// Else add the current character to the new string
else {
n += s.charAt(i);
}
}
// We haven't checked the last character yet. Let's add it to the string
// if 'rem' is false.
if (!rem) {
n += s.charAt(s.length() - 1);
}
return n;
}
请注意,此代码平均比正则表达式快三倍以上。
答案 2 :(得分:0)
考虑这个更容易理解的代码
String s1="aabcccdee";
while (true) {
rvpoint:
for (int x = 0; x < s1.length() -1; x++)
{
char c = s1.charAt(x);
if (c == s1.charAt(x+ 1)) {
s1 = s1.replace(String.valueOf(c), "");
continue rvpoint; // keep looping if a replacement was made
}
}
break; // break out of outer loop, if replacement not found
}
System.out.println(s1);
注意强>
这只适用于第一次迭代,放入方法并继续调用,直到大小不变为
答案 3 :(得分:0)
尝试这样的事情:
public static void print() {
String s1 = "abcccbd"; // I have taken a sample test case
String s2 = "";
while (!s1.equals(s2)) {
s2 = s1;
s1 = s1.replaceAll("(.)\\1+", "");
}
System.out.println(s1);
}