/** Return a list of all items in L that appear more than once.
* Each item appears once in the result.
*/
static List<String> duplicates(List<String> L) {
ArrayList<String> result = new ArrayList<String>();
int n;
n = 0;
for (ListIterator<String> p1 = L.listIterator(); p1.hasNext();
n += 1) {
String x = p1.next();
if (result.contains(x)) {
continue;
}
int m;
m = L.size() - 1;
for (ListIterator<String> p2 = L.listIterator(L.size());
m > n; m -= 1) {
if (x.equals(p2.previous())) {
result.add(x);
break;
}
}
}
Collections.sort(result);
return result;
}
我正在尝试修改此代码,以便不使用除结果、p1 和 p2 之外的任何其他变量。这就是我现在所拥有的,但我对如何解决这个问题非常迷茫。
ListIterator<String> p1 = L.listIterator();
while (p1.hasNext()) {
String x = p1.next();
if result.contains(x)) {
continue;
}