[LeetCode]Remove Duplicates from Sorted Array
来源:互联网 发布:Mac版本好玩的网络游戏 编辑:程序博客网 时间:2024/06/12 18:00
题目:
Remove Duplicates from Sorted ArrayGiven 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]
.
来源:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/
思路:
因为是有序的数组,只要前后指针,一次遍历就行。
C++ AC代码:
class Solution {public: int removeDuplicates(int A[], int n) { if( n < 2) return n;int i=0, j=1;while( i < n && j < n ){ if( A[i] == A[j] ) j++;else A[++i] = A[j++];}return i+1; }};
运行时间 116ms
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
- 手机卫士第七天知识点
- java面向对象-jar包
- 大数浮点数加法
- iOS 画画板
- pycharm 3.4.1 安装
- [LeetCode]Remove Duplicates from Sorted Array
- FineUI Grid不显示数据解决方案
- Nginx使用教程一(安装与配置)
- GetDIBits使用方法
- UVA 10780-Again Prime? No Time. (数学-质因子)
- java多线程-概述-创建线程_继承Thread类-run和start特点
- EL 表达式中自定义函数
- 冒泡排序
- 大数进制转化