我已经测试了一段时间的不同方法,这就是我得到的。我只是不知道如何解决这个问题。
/** Return true iff s has an odd number of characters and
* the substring before the middle character equals the substring
* after it.
* Examples: For s = "" return false
* For s = "b" return true
* For s = "xbx" return true
* For s = "xxxx" return false
* For s = "hellohello" return false
* For s = "hello!hello" return true */
public static boolean isDoubled(String s) {
// TODO 1. There is no need for a loop. Do not use a loop.
// In all methods, use s1.equals(s2) and NOT s1 == s2 to test
// equality of s1 and s2.
int midLen = s.length() / 2;
if (midLen == 0) return false;
String[] parts = {s.substring(0, midLen - 1), s.substring(midLen + 1)};
String part1 = parts[0];
String part2 = parts[1];
if ((s.length() % 2 == 0) && (part1.equals(part2))) {
return true;
}
return false;
}
HUWWW
森林海
ibeautiful
相关分类