## 一.题目描述
![](https://box.kancloud.cn/2016-01-05_568bb5e8b10a0.jpg)
## 二.解题技巧
从题目中可知,数组中的元素事先已经过排序,因此一个简单而易于实现的方法是从第二个元素开始对数组元素进行遍历,并判断该元素是否和前面的元素相同。题目要求返回不重复的元素的个数。
算法的一个要求是:返回的数组的元素都是不重复,同时要求remove duplicates in place,这意味着不能重新定义一个数组来保存不重复的元素。假设该数组为A,为了满足这一要求,可以设置一个变量num来保存不重复的元素的个数,然后遍历整个数组A,如果该元素A[i]!=A[i-1]的话,将第i个元素复制到A的num位置,也即是A[num]=A[i],然后将num加1.
主要的注意点是,如果数组的元素个数小于2的话,就可以提前结束函数了。
## 三.示例代码
~~~
#include <iostream>
using namespace std;
class Solution {
public:
int removeDuplicates(int A[], int n)
{
if (n < 2) return 0; // 数组元素个数小于2时无需执行算法
int num = 0;
for (int i = 1; i < n; i++) {
if (A[num] != A[i])
A[++num] = A[i];
}
return num + 1;
}
};
~~~
测试代码:
~~~
#include "solution.h"
#include <algorithm>
int main()
{
int num = 50;
int number[50];
for (int i = 0; i < num; i++)
number[i] = rand() % 10 - 10;
sort(number, number + num); // 先对数组进行排序
cout << "输入的数组(已排序):" << endl;
for (int j = 0; j < num; j++)
cout << number[j] << " ";
cout << endl << endl;
Solution remove;
int index = remove.removeDuplicates(number, num); // 执行算法
cout << "去除重复元素后的数组:" << endl;
for (int k = 0; k <index; k++)
cout << number[k] << " ";
cout << endl;
cout << "数组元素的剩余个数:" << index << endl;
getchar();
return 0;
}
~~~
一个输出结果:
![](https://box.kancloud.cn/2016-01-05_568bb5e8d2d83.jpg)
**网上一种使用STL的解决思路如下:**
~~~
// 算法的时间复杂度O(n),空间复杂度O(1)
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;
}
};
~~~
## 四.体会
这道题本身无需排序,只需判断元素与前一个元素是否相同就知道该元素是不是重复的,同时,记录已经遍历过的元素中不重复的元素的个数的变量也同时记录了下一个不重复的元素在数组中的存放位置。算法实现较为简单,当然,可能存在其他更简单的思路。
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching