File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode id=26 lang=cpp
3
+ *
4
+ * [26] Remove Duplicates from Sorted Array
5
+ */
6
+
7
+ // @lc code=start
8
+ class Solution {
9
+ public:
10
+ int removeDuplicates (vector<int >& nums) {
11
+
12
+ if (nums.size () == 0 )
13
+ return 0 ;
14
+
15
+ int i = 0 ; // index of the current element
16
+ int j = 0 ; // index of the next element
17
+ while (j < nums.size ()) // while there are still elements to compare
18
+ {
19
+ if (nums[i] != nums[j]) // if the current element is not the same as the next element
20
+ {
21
+ i++; // increment the index of the current element
22
+ nums[i] = nums[j]; // assign the next element to the current element
23
+ }
24
+ j++; // increment the index of the next element
25
+ }
26
+ return i + 1 ;
27
+ }
28
+ };
29
+ // @lc code=end
30
+
You can’t perform that action at this time.
0 commit comments