## 一.題目描述

## 二.解題技巧
這道題和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;
}
~~~
一個測試結果:

## 四.體會
這道題實現簡單,沒有很嚴格的邊界條件,可以尋求別的解決方式。
- 前言
- 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