Problem:
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 are you leave beyond the new length.
Solution: As with the 26 questions, the judging conditions are different.
To an array, it is required to return an array that deletes all the specified elements.
Good chicken jelly, the first time all through, no one error (although the topic is relatively simple) ... Map Souvenir:
Java source Code (248MS):
public class Solution {public int removeelement (int[] nums, int val) { int size=0,length=nums.length; for (int i=0;i<length;i++) { if (nums[i]!=val) nums[size++]=nums[i]; } return size; }}
C Language Source code (2MS):
int removeelement (int* nums, int numssize, int val) { int size=0,i; for (i=0;i<numssize;i++) { if (nums[i]!=val) nums[size++]=nums[i]; } return size;}
C + + source code (5MS):
Class Solution {public: int removeelement (vector<int>& nums, int val) { int size=0,length=nums.size ( ); for (int i=0;i<length;i++) { if (nums[i]!=val) nums[size++]=nums[i]; } return size; };
Python source code (64MS):
Class solution: # @param {integer[]} nums # @param {integer} val # @return {integer} def removeelement ( Self, Nums, val): Size=0;length=len (nums) for i in range (length): if nums[i]!=val:nums[size]=nums[i]; Size+=1 return size
Leetcode Remove Element (C,c++,java,python)