[LeetCode] Remove Duplicates from Sorted Array
LeetCode from Array remove sorted Duplicates
2023-09-11 14:17:25 时间
Given a sorted array, remove the duplicates in place such that each element appear only once
and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example, Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
思路:
1 index 指向的是没有重复的递增的数组的最后一个,index后面的i, 使得A[i] != A[index], 然后index++, i++
1 class Solution { 2 public: 3 int removeDuplicates(int A[], int n) { 4 if(n == 0) 5 return 0; 6 int index = 0; 7 for(int i = 1; i< n;i++) 8 { 9 if(A[index] != A[i]) 10 { 11 index++; 12 A[index] = A[i]; 13 } 14 } 15 return index + 1; 16 } 17 };
相关文章
- Leetcode 之Length of Last Word(37)
- Java实现 LeetCode 799 香槟塔 (暴力模拟)
- Java实现 LeetCode 556 下一个更大元素 III(数组的翻转)
- Java实现 LeetCode 318 最大单词长度乘积
- Java实现 LeetCode 164 最大间距
- Java实现 LeetCode 7整数反转
- (LeetCode 82)Remove Duplicates from Sorted List II
- LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium
- (LeetCode 83)Remove Duplicates from Sorted Lists
- [ES6] Converting an array-like object into an Array with Array.from()
- [LeetCode] Single Number II
- [LeetCode] Find Median from Data Stream
- LeetCode(58): 最后一个单词的长度
- LeetCode-121. 买卖股票的最佳时机【单调栈,一次遍历】==注意暴力解法会超时。==
- leetcode 题解 || Remove Nth Node From End of List 问题
- Leetcode 2264. 字符串中最大的 3 位相同数字(可以,已解决)
- Leetcode 999. 可以被一步捕获的棋子数
- Leetcode 345. 反转字符串中的元音字母
- Leetcode 73. 矩阵置零
- [LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)
- leetcode 26 Remove Duplicates from Sorted Array
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
- leetcode 606. Construct String from Binary Tree
- 【Leetcode刷题Python】20. 有效的括号
- 【LeetCode】279. 完全平方数