We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 17f22d5 commit 8a12b05Copy full SHA for 8a12b05
algorithms/remove-duplicates-from-sorted-array.js
@@ -4,20 +4,19 @@
4
* @return {number}
5
*/
6
var removeDuplicates = function (nums) {
7
- // 双指针
8
- if (nums.length === 0) {
9
- return 0;
+ // 双指针 (快慢指针)
+
+ if (nums.length <= 1) {
10
+ return nums.length;
11
}
- let slow = 1;
12
- let fast = 1;
13
-
14
- while (fast < nums.length) {
15
- if (nums[fast] !== nums[fast - 1]) {
16
- nums[slow] = nums[fast];
17
- slow++;
+ let slowIndex = 1;
+ for (let fastIndex = 1; fastIndex < nums.length; fastIndex++) {
+ if (nums[fastIndex - 1] !== nums[fastIndex]) {
+ nums[slowIndex++] = nums[fastIndex];
18
19
- fast++;
20
21
22
- return slow;
+ return slowIndex;
23
};
0 commit comments