Leetcode Remove Element (C,c++,java,python)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.