I'm trying to do this problem from CodingBat but don't understand why it doesn't work with input string "Hello!".
This is my code and below my code are the results I'm getting.
window.CurrentFileUploaded = function (radAsyncUpload, args) {
...
if (navigator.userAgent.indexOf("Firefox") != -1) // if FireFox, delay 10 ms. This is the fix for FireFox bug!!!
{
setTimeout(function () { showThumbnail(...); }, 10);
}
else
{
showThumbnail(...);
}
}
答案 0 :(得分:4)
Your problem is that you are both incrementing static_cast<Star*>(data_)->PrintToConsole(); // Haha, Star star!
and using i
. When start.length()-i
is equal to 1, the i
variable becomes one character shorter. But when start
is 2, i
is already one less than the original, and now you subtract 2 characters, so now you've missed one. The same is true for the start.length()
variable. Don't use both an incrementing end
and the changing length of the strings.
To fix it, don't change the original i
and start
variables. Do something like this:
end
答案 1 :(得分: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 */
}
答案 2 :(得分:0)
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;
}
希望它可以帮到你
答案 3 :(得分: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 "";
}
答案 4 :(得分:0)
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);
}
答案 5 :(得分:0)
我的解决方案:
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 "";
}