Leetcode:Remove Duplicates from Sorted Array
来源:互联网 发布:wlan 数据分流技术 编辑:程序博客网 时间:2024/05/16 23:37
题目
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].
我的解法
一开始我忽略掉sorted array
这个条件,所以导致我需要进行循环遍历,时间复杂度为
我的错误代码:
int removeDuplicates(int A[], int n) { int i, j, num, mark; num = 0; for (i = 0; i < n; i++){ mark = 0; for (j = 0; j < num; j++){ if (A[j] == A[i]) mark++; else ; } if (mark > 0) ; else A[num++] = A[i]; } return num;}
修改后的代码:
【和Remove Element那道题思路一致,采用双指针!!】
int removeDuplicates(int A[], int n) { int i, num; num = 1; if (n < 2) return n; for (i = 1; i < n; i++){ if (A[i] != A[i - 1]) A[num++] = A[i]; } return num;}
0 0
- [leetcode][array] Remove Duplicates from Sorted Array
- 【Leetcode】【Array】Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- leetcode 35: Remove Duplicates from Sorted Array
- [LeetCode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array
- Leetcode: Remove Duplicates from Sorted Array
- [leetcode] Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array
- [LeetCode]Remove Duplicates from Sorted Array II
- [leetcode]Remove Duplicates from Sorted Array
- [leetcode]Remove Duplicates from Sorted Array II
- 破解linux root密码
- UEditor | 百度富文本使用
- iOS开发之:NSNotificationCenter(通知)的使用方法
- XCode 6.1 自动补全失效
- Android获取经纬度、计算距离、方位角
- Leetcode:Remove Duplicates from Sorted Array
- C语言运行时库详解 (C/C++ Runtime)
- fedora20/21 中文输入法 记录
- SEED实验系列:ShellShock 攻击实验
- js获取关闭浏览器事件
- Docker 快速上手系列(1): 镜像的概念及相关操作
- android TextView改变部分字体的颜色,大小,设置部分字体有点击事件
- R语言 | 单元变量的数据可视化方法
- OWLQN理解