Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8c56452

Browse files
fix 380
1 parent3718532 commit8c56452

File tree

1 file changed

+7
-5
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+7
-5
lines changed

‎src/main/java/com/fishercoder/solutions/_380.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ public boolean remove(int val) {
3636
if (!map.containsKey(val)) {
3737
returnfalse;
3838
}else {
39-
intlastElement =list.get(list.size() -1);
40-
intindex =map.get(val);
41-
list.set(index,lastElement);
42-
map.put(lastElement,index);
43-
list.remove(list.size() -1);
39+
intremoveIndex =map.get(val);
40+
if (removeIndex !=list.size() -1) {//if it's not the last element, then we need to swap it with the last element so that this operation is also O(1)
41+
intlastElement =list.get(list.size() -1);
42+
list.set(removeIndex,lastElement);
43+
map.put(lastElement,removeIndex);
44+
}
4445
map.remove(val);
46+
list.remove(list.size() -1);
4547
returntrue;
4648
}
4749
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp