- Use Set
- Insert in Set: NLog(N) + Time O(N)
- Space O(N)
- 2 Pointer
-
fun(){ int fP = 0; int sP = 1; for(int sP=1, sP<n;sP++){ if(a[fP] != a[sP]) { a[fP+1] = a[sP]; fP++; } } return fP+1; }
-
Time: O(N)
Space: O(1)
Search
Oct 07, 20251 min read
fun(){
int fP = 0;
int sP = 1;
for(int sP=1, sP<n;sP++){
if(a[fP] != a[sP])
{
a[fP+1] = a[sP];
fP++;
}
}
return fP+1;
}Time: O(N)
Space: O(1)