每日一道算法题 移除元素

题目

27. 移除元素 - 力扣(LeetCode)

Python

class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        while val in nums:
            for num in nums:
                if num==val:
                    nums.remove(num)
                    break
        return len(nums)
class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        ans=0
        for num in nums:
            if num!=val:
                nums[ans]=num
                ans+=1
        return ans

 

C++

class Solution {
public:
    int removeElement(vector& nums, int val) 
    {
        int ans=0;
        for(int num:nums)
        {
            if(num!=val) nums[ans++]=num;
        }
        return ans;
    }
};

C语言

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

你可能感兴趣的:(算法题,算法,数据结构)