进入OJ
Description
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]
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]
Input
多组测试数据,每组数据两行。
第一行输入数字n(n>0);
第二行输入n个已经排序好的数字。
第一行输入数字n(n>0);
第二行输入n个已经排序好的数字。
Output
对每组测试数据输出不重复数字的个数,每组数据占一行。
Sample Input
3
2 2 3
2 2 3
Sample Output
2
Hint
题目大意:输入一组已经排序好的数字,计算出不重复的数字个数。
线性表,数组
线性表,数组
Source
leetcode
Code
#include <stdio.h> #include <algorithm> #include <vector> using namespace std; int main() { int n; while(scanf("%d",&n)!=EOF) { vector<int> a(n,0); for(int i =0;i<n;i++) scanf("%d",&a[i]); int index = 0; for (int i = 0; i < n; i++) { if (a[index] != a[i]) a[++index] = a[i]; } printf("%dn", index + 1); } }
代码2
class Solution { public: int removeDuplicates(int A[], int n) { return distance(A, unique(A, A + n)); } };
代码3
class Solution { public: int removeDuplicates(int A[], int n) { return removeDuplicates(A, A + n, A) - A; } template<typename InIt, typename OutIt> OutIt removeDuplicates(InIt first, InIt last, OutIt output) { while (first != last) { *output++ = *first; first = upper_bound(first, last, *first); } return output; } };
都是技术利害的人。
博主就是传说中的js高手吗