【问题标题】:Codingbat plusOut - StringBuffer TroubleCodingbat plusOut - StringBuffer 麻烦
【发布时间】:2015-04-27 16:15:00
【问题描述】:

给定一个字符串和一个非空字符串,返回原始字符串的一个版本,其中所有字符都被加号(“+”)替换,除了保持不变的单词字符串的外观。

plusOut("12xy34", "xy") → "++xy++"
plusOut("12xy34", "1") → "1+++++"
plusOut("12xy34xyabcxy", "xy") → "++xy++xy+++xy"

我在尝试编写 StringBuffer 解决方案来解决这个问题时遇到了麻烦。 这是这个的原始代码:

public static String plusOut(String str, String word) {
    int i = 0; 
    String str2 = "";
    while (i < str.length() - word.length()+1) 
        if (!str.substring(i,i+word.length()).equals(word)) {
            str2 += "+";
            i++;
        }
        else {
            str2 += word;
            i += word.length(); //found pattern - skip
        }
    //if any remaining chars at end (guaranteed not to be pattern) replace    
    //with +s
    if (i < str.length() && !str.substring(i).equals(word.substring(1))) {
        for (int j = 0; j < word.length()-1; j++) str2 += "+";
    }
    return str2;
}

【问题讨论】:

  • 您的问题是什么?我没有看到StringBuffer
  • 使用递归。图灵机
  • @JaredBurrows 我想将我的代码转换为使用 StringBuffer 的位置

标签: java stringbuffer


【解决方案1】:
public String plusOut(String str, String word) {
      String result = "";
      int i = 0 ;

      while(i < str.length() ) {
         if (str.substring(i).startsWith(word)) {
            result = result + word;
            i = i + word.length();
         } else {
            result = result + "+" ;
            i++;
         }
      }

      return result ;
}

//字符串生成器

public String plusOut(String str, String word) {
    StringBuilder strBuilder = new StringBuilder();
    int i = 0 ;

      while(i < str.length() ) {
         if (str.substring(i).startsWith(word)) {
            strBuilder.append(word);
            i = i + word.length();
         } else {
            strBuilder.append("+");
            i++;
         }
      }

      return strBuilder.toString();

}

【讨论】:

    【解决方案2】:

    这是我想出的:

      public String plusOut(String str, String word) 
      {
        //Create a blank string.
        String out = "";
        //Create a counter to get the end amount.
        int endAmt = 0;
        //We can only manipulate from 0 to str.length() - word.length() in this for loop, because
        //when searching for word in str, we can't go beyond the bounds.
        for (int i = 0; i < str.length() - word.length(); i++)
        {
          //If we find word in str,
            if (str.substring(i, i + word.length()).equals(word))
            {
              //We append word and increment 2 times further.
              out = out + str.substring(i, i + word.length());
              //increment i by 2 (one here, and one at the  end)
              i += word.length() - 1;
              //We set the endAmt equal to the next iteration for future use when getting the rest
              //of the amount later.
              endAmt = i + 1;
            }
            //if it doesn't match, we change the character to a '+'
            else
            {
              out = out.substring(0,i) + '+';
            }
        }
        //Now, we finish the string after the searching for word in str.
        //If the last substring is word, we add word to the mix.
        if (str.substring(str.length() - word.length()).equals(word))
        {
          out = out + word;
        }
        //If it's not, then we refactor the string to after then last instance of word
        else
        {
          out = out.substring(0, endAmt);
          //and then we add the remaining amount of +'s
          for (int i = 0; i < str.length() - endAmt; i++)
          {
            out = out + "+";
          }
        }
        //finally, we return the string.
        return out;
      }
    

    【讨论】:

    • 我对编程还很陌生,所以我确信有一个更简单的答案。
    【解决方案3】:

    这是一个非常简单易懂的代码

    public static String plusOut(String str,String word){
        StringBuffer b = new StringBuffer();
        int indexOfWord = str.indexOf(word, 0);
        for (int i = 0; i < str.length(); i++) {
            if(i==indexOfWord){
                b.append(word);
                i=i+word.length()-1;//move index by word length 
                //get next index for the word
                indexOfWord = str.indexOf(word,indexOfWord+word.length());
            }else{
                b.append("+");
            }
        }
        return b.toString();
    }
    

    【讨论】:

      【解决方案4】:
      public String plusOut(String str, String word) {
        String temp = str.replace(word, "+");
        String newStr = "";
      
        for (int i=0; i<temp.length(); i++)
        {
          if (temp.charAt(i) == '+')
          {
            newStr += word;
          }
          else
          {
            newStr += "+";
          }
        }
      
        return newStr;
      }
      

      【讨论】:

        【解决方案5】:
        public String plusOut(String str, String word) {
          
          String result = "";
          
          for (int i = 0; i < str.length(); i++){
            if (str.substring(i).length() >= word.length() 
            && str.substring(i, i + word.length()).equals(word)) {
              
            result += word;
            i += word.length() - 1;
            }
            else if (str.length() < word.length() 
            || str.substring(i).length() < word.length())
              result += '+';
            else
            result += '+';
          }
          
        return result;
        }
        

        【讨论】:

          【解决方案6】:
          public String plusOut(String str, String word) {
            String newStr = "";
            int sLength = str.length();
            int wLength = word.length();
            
            for (int i = 0; i < sLength; i++){
              if (i <= (sLength-wLength) && str.substring(i, i+wLength).equals(word)){
                newStr += word;
                i += (wLength-1);
                continue;
              }
              newStr += "+";
            }
            return newStr;
          }
          

          【讨论】:

          • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
          • @taha 谢谢!我是堆栈溢出的新手,所以我仍然习惯它的要点。
          猜你喜欢
          • 2010-11-11
          • 2011-06-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多