【问题标题】:CodingBat sameEnds dealing with StringsCodingBat 同样结束处理字符串
【发布时间】:2016-05-24 04:50:48
【问题描述】:

我正在尝试解决来自 CodingBat 的 this 问题 但不明白为什么它不适用于输入字符串“Hello!”。

这是我的代码,下面是我得到的结果。

public String sameEnds(String string) {
        String result;
        int strLen = string.length();
        boolean isOdd = (strLen % 2 != 0);
        String start = string.substring(0, strLen / 2);
        String end;
        if (isOdd) {
            end = string.substring(strLen / 2 + 1);
        } else {
            end = string.substring(strLen / 2);
        }
        int i = 0;
        while (!start.equals(end) && i <= start.length()) {
            start = start.substring(0, start.length() - i);
            end = end.substring(i, end.length());
            i++;
        }
        if (start.equals(end)) {
            result = start;
        } else {
            result = "";
        }
        return result;
    }

【问题讨论】:

    标签: java string substring


    【解决方案1】:

    您的问题是您既要增加 i 又要使用 start.length()-i。当i 等于1 时,start 变量会缩短一个字符。但是当i 是2 时,start.length() 已经比原来的少了1,现在你减去2 个字符,所以现在你漏掉了一个。 end 变量也是如此。不要同时使用递增的i 和不断变化的字符串长度。

    要修复它,请不要更改原始的 startend 变量。做这样的事情:

        String sTmp = start;
        String eTmp = end;
        while (!sTmp.equals(eTmp) && i <= start.length()) {
            sTmp = start.substring(0, start.length() - i);
            eTmp = end.substring(i, end.length());
            i++;
        }
        if (sTmp.equals(eTmp)) {
            result = sTmp;
        } else {
            result = "";
        }
        return result;
    

    【讨论】:

    • 感谢您的帮助。但是,例如,我的代码如何为字符串“javaXYZjava”工作?
    • 它只是碰巧在这种情况下工作,因为跳过字母不会导致它失败。但它不适用于像“javaWXYZjava”这样的额外字符。
    【解决方案2】:

    您的代码似乎过于复杂。考虑一下:

    public String sameEnds(String string) {
       int e = string.length() - 1;                  /* end of string */
       int b = string.length() / 2;                  /* where to start looking for a match */
    
       while (--b >= 0) {                            /* ran off the front yet? */
    
           /*
            * Starting just below the center of the string,
            * look for a character which matches the final character
            */
           for ( ; b >= 0; --b) {
              if (string.charAt(e) == string.charAt(b)) break;
           }
    
           /*
            * found a match to the final character (a possible starting point)
            * compare characters backwards until no match or all matched
            * (temp vars ee and bb walk backwards from e and b respectively)
            *
            *    "|f|r|o|b|o|z|z|Q|Q|Q|f|r|o|b|o|z|z|"
            *                  ^                   ^
            *                  |                   |
            *         <--bb    b          <--ee    e
            */
           for (int ee = e, bb = b; bb >= 0; --bb, --ee) {
              if (string.charAt(bb) != string.charAt(ee)) break;  /* no match */
              if (bb == 0) return string.substring(0, b+1);       /* victory! */
           }
       }
       return new String("");                              /* nothing matched */
    }
    

    【讨论】:

    • 是的,谢谢。我有点初学者,所以我的方法不是最有效的。我只是在写我想到的逻辑。
    • @Nico 我这样做的时间越长,我就越相信 90% 的编程确实在定义问题。希望我的 cmets 和图片能让您了解我对这个的想法……怎么会有匹配?开头的东西必须匹配最后一个字符。在哪里?它必须在字符串的前半部分。找一个?现在开始比较...失败,好的,向左寻找与最后一个字符匹配的另一个字符并重复...
    【解决方案3】:

    这是我的解决方案

    public String sameEnds(String string) {
      int mid = string.length() / 2;
      String ls = string.substring(mid);
      String result = "";
      int index = -1;
      int i = 0;
    
      if (string.length() < 2) {
        return "";
      }
    
      if (string.length() % 2 == 1) {
        i = 1;
      }
    
      // All we need to do is loop over the second part of the string,
      // find out the correct substring that equals to the origin string which start the index of '0'.
    
      for (; i < ls.length(); i++) {
        index = string.indexOf(ls.substring(i));
        if (index == 0) {
          result += ls.substring(i);
          break;
        }
      }
    
      return result;
    }
    
    希望它可以帮助你

    【讨论】:

    • 你应该添加一个解释。
    【解决方案4】:
    public String sameEnds(String string) {
      //First half of the string
      String result = string.substring(0, string.length() / 2); 
      for (int i = 0; i < string.length() / 2; i++)
      {
        //check if first half equals second half
        if (result.equals(string.substring(string.length() - result.length())))
            //return result if true
            return result;
        //else remove last character of first half
        result = result.substring(0, result.length() - 1);
      }
      //return empty string if ends are not equal
      return "";
    }
    

    【讨论】:

      【解决方案5】:
      public String sameEnds(String string) {
        final int len = string.length();
        String commonEnd = ""; 
      
        //Return empty for Anything smaller than size 1 or size 1 
        if (len <= 1) return commonEnd;
      
        //    S T R I N G
        //    0 1 2 3 4 5
        // left = 2, right = 3
        int left = len / 2;
        int right = len % 2 == 0 ? (len / 2) : (len / 2) + 1;
      
        while (left > 0) {
          //Start by comparing the string cut down in biggest halves and then keep squeezing
          //left and right from there on
          if (string.substring(0, left).equals(string.substring(right, len))) {
            break;
          }
            left--;
            right++;
        }
      
        return string.substring(0, left);
      }
      

      【讨论】:

        【解决方案6】:

        我的解决方案:

        public String sameEnds(String str) {
        
            int middle = str.length()/2;
        
            for (int i = middle; i >= 0; i--) {
                String left = str.substring(0,i);
        
                if (str.endsWith(left)) {
                    return left;
                } 
        
                }
        
              return "";
        }
        

        【讨论】:

          猜你喜欢
          • 2013-06-10
          • 2015-06-13
          • 1970-01-01
          • 2015-08-16
          • 2015-10-03
          • 1970-01-01
          • 1970-01-01
          • 2022-11-16
          • 2018-01-13
          相关资源
          最近更新 更多