[LeetCode] 27. Remove Element

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

Do not allocate extra space for another array, you must do this in place with constant memory.spa

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

Example:
Given input array nums = [3,2,2,3]val = 3blog

Your function should return length = 2, with the first two elements of nums being 2.element

 

 1 int removeElement(int* nums, int numsSize, int val) {
 2     int cur = 0;
 3     
 4     if (numsSize == 0){
 5         return 0;
 6     }
 7     
 8     for (int i = 0; i < numsSize; i++){
 9         if (nums[i] != val){
10             nums[cur++] = nums[i];
11         }
12     }
13     
14     return (cur);
15     
16 }
相關文章
相關標籤/搜索