Leetcode------Remove Element

Source: Internet
Author: User

Title: Remove Element
Pass Rate: 32.6%
Difficulty: Simple

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.

I do not know whether it is my understanding of the problem, or the topic is not clear, according to the return of the request, only to remove the same element with Elem after the length of the array to convert to a new array what is the meaning of it? I feel that the direct statistics of the same elements as elem and then the length of a array minus is the length of the conversion, but after submission is not prompt. I don't know, either. So I switched over, two pointers to the operation, one is I, the other is J. I is to follow the sequence, if elem= the current position of the element, J do not go, I continue to go, so that the same as elem to cover off, the last J is the length of the new array.

1  Public classSolution {2      Public intRemoveelement (int[] A,intelem) {3         intLen=a.length,j=0;4          for(inti=0;i<len;i++){5             if(a[i]!=elem) {6a[j]=A[i];7J + +;8             }9         }Ten         returnJ; One     } A}

Leetcode------Remove Element

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.