------解决方案-------------------- public static String delRepeatStr(String s){ char[] chrArr = s.toCharArray(); byte []flagArr = new byte[chrArr.length]; for (int i = 0; i < flagArr.length; i++) { flagArr[i] = 0; //0:保留;1:不保留 }
for (int i = 0; i < chrArr.length; i++) { int j = i - 1; while (true) { if (j < (i - j - 1) || flagArr[j] == 1) break;
boolean isRepeat = true; for (int k = 0; k < i - j; k++) { if (chrArr[i - k] != chrArr[j - k]) { isRepeat = false; break; } }
if (isRepeat) { for (int k = 0; k < i - j; k++) { flagArr[i - k] = 1; flagArr[j - k] = 1; } }
j--; }
}
int remainCharCount = 0; for (int i = 0; i < chrArr.length; i++) if (flagArr[i] == 0) remainCharCount++;
char[] chrArrRem = new char[remainCharCount]; int index = 0; for (int i = 0; i < chrArr.length; i++) { if (flagArr[i] == 0) { chrArrRem[index++] = chrArr[i]; } }