Skip to content

Commit b9ab3a4

Browse files
authored
Update 448. Find All Numbers Disappeared in an Array.java
1 parent 04cf27a commit b9ab3a4

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

448. Find All Numbers Disappeared in an Array.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,20 @@ public List<Integer> findDisappearedNumbers(int[] nums) {
1919
return out;
2020
}
2121
}
22+
23+
// Consider working directly on the input to achieve O(1) space
24+
List<Integer> result = new ArrayList<Integer>();
25+
for( int i=0;i< nums.length; i++){
26+
int index = nums[i];
27+
if(nums[Math.abs(index)-1] > 0){
28+
nums[Math.abs(index)-1]= -nums[Math.abs(index)-1];
29+
}
30+
}
31+
32+
for(int j =1 ;j <= nums.length ; j++){
33+
if(nums[j-1] > 0){
34+
result.add(j);
35+
}
36+
}
37+
return result;
38+
}

0 commit comments

Comments
 (0)