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:

和26题一样,就是判断条件不一样而已。

题目大意:

给一个数组,要求返回删除所有指定元素的数组。

好鸡冻,第一次全部通过,没有一个错误(虽然题目比较简单)。。。。。。
贴图留念:

Java源代码(248ms):

public class Solution {
    public int removeElement(int[] nums, int val) {
        int size=0,length=nums.length;
        for(int i=0;i

C语言源代码(2ms):

int removeElement(int* nums, int numsSize, int val) {
    int size=0,i;
    for(i=0;i

C++源代码(5ms):

class Solution {
public:
    int removeElement(vector& nums, int val) {
        int size=0,length=nums.size();
        for(int i=0;i

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


你可能感兴趣的:(LeetCode)