Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

26. Remove Duplicates from Sorted Array

Posted on 2020-06-19 | Edited on 2021-01-22

LeetCode

Approach: Two Pointers, Time complextiy : O(n)

1
2
3
4
5
6
7
8
9
10
class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length<2) return 1;
int j=0;
for(int i=1;i<nums.length;i++){
if(nums[i]!=nums[i-1]) nums[++j]=nums[i];
}
return ++j;
}
}

<1…145146147…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%