LeetCode 27 Remove Element (C,C++,Java,Python)
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 you leave beyond the new length.
Solution:
题目大意:
Java源代码(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语言源代码(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++源代码(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源代码(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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。