Remove Element

描述

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

分析

代码

  1. // Remove Element
  2. // Time Complexity: O(n), Space Complexity: O(1)
  3. class Solution {
  4. public:
  5. int removeElement(vector<int>& nums, int target) {
  6. int index = 0;
  7. for (int i = 0; i < nums.size(); ++i) {
  8. if (nums[i] != target) {
  9. nums[index++] = nums[i];
  10. }
  11. }
  12. return index;
  13. }
  14. };

相关题目

原文: https://soulmachine.gitbooks.io/algorithm-essentials/content/cpp/linear-list/array/remove-element.html