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. public class Solution {
  4. public int removeElement(int[] nums, int target) {
  5. int index = 0;
  6. for (int i = 0; i < nums.length; ++i) {
  7. if (nums[i] != target) {
  8. nums[index++] = nums[i];
  9. }
  10. }
  11. return index;
  12. }
  13. };

相关题目

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