CodingBat sameEnds处理字符串

问题描述 投票:3回答:4

我正在尝试从CodingBat做this问题,但不明白为什么它不能用输入字符串“你好!”。

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

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
4个回答
4
投票

你的问题是你正在增加i和使用start.length()-i。当i等于1时,start变量变短一个字符。但是当i为2时,start.length()已经比原来少了一个,现在你减去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;

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 */
}

0
投票

Here's my solution

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;
}
Hope it can help you

0
投票
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 "";
}
© www.soinside.com 2019 - 2024. All rights reserved.