## 一.题目描述
![](https://box.kancloud.cn/2016-01-05_568bb5e97947b.jpg)
## 二.解题技巧
这道题和Remove Duplicates from Sorted Array类似,不同的是这里只要删除值等于给定值的元素即可,可以采用和前面的题相同的做法:可以将原来的数组看作一个栈,设定一个栈顶指针,在对数组进行遍历的时候,判断元素是否等于给定值,如果等于,则直接进行数组的下一个元素,如果不等于,则将该元素放入到栈顶,然后更新指针并处理数组的下一个元素。之所以能这么做,是因为我们在遍历过程中,对于数组元素的访问的下标总是不小于栈顶指针的,因此,可以将原来的数组直接当作一个栈来使用,这样就能做到in-place处理了。
这道题相对比较简单,也没有多少边界条件要考虑,是一道练手的题。这里在进行删除后数组的元素的个数的统计的时候,有一个技巧,即栈顶指针就已经说明了数组中元素的个数了,因此,可以直接返回栈顶指针就可以了(这里的栈顶指针是用数组的下标表示的)。当然,也可以统计等于给定值的元素的个数,然后用原来的数组个数减去该值就可以得到新的数组的值了。
## 三.示例代码
~~~
#include <iostream>
using namespace std;
class Solution
{
public:
int RemoveElement(int A[], int n, int Elem)
{
int num = 0;
for (int i = 0; i < n; i++)
{
if (A[i] != Elem)
A[num++] = A[i]; // 支持in place运算
}
return num;
}
};
~~~
测试代码:
~~~
#include "Solution.h"
#include <algorithm>
int main()
{
int Elem = 5; // 删除数组内数值为5的元素
int length = 100; // 数组大小
int A[100];
for (int i = 0; i < length; i++)
A[i] = rand() % 20 - 10;
sort(A, A + length); // 便于观察,对输入数组先进行排序
cout << "输入数组长度为:" << length << endl;
cout << "输入数组为:" << endl;
for (int j = 0; j < 100; j++)
cout << A[j] << " ";
cout << endl << endl;
cout << "删除数值为:" << Elem << "的元素" << endl << endl;
Solution s;
int result = s.RemoveElement(A, 100, Elem);
cout << "输入数组长度为:" << result << endl;
cout << "输出数组为:" << endl;
for (int k = 0; k < result; k++)
{
cout << A[k] << " ";
}
cout << endl;
getchar();
return 0;
}
~~~
一个测试结果:
![](https://box.kancloud.cn/2016-01-05_568bb5e9904aa.jpg)
## 四.体会
这道题实现简单,没有很严格的边界条件,可以寻求别的解决方式。
- 前言
- 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