[Leetcode] Remove Element Analysis

Source: Internet
Author: User

The Remove element is a water problem for leetcode, but there are a number of ways to do this, and now I'm going to do a little discussion about some of the things I know.

Title Link: https://leetcode.com/problems/remove-element/

Title Description: 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.
It is important to note that "It doesn ' t matter what do you leave beyond the new length."

Thought analysis: The idea is very simple, directly on the code.

Code One: Violent movement

1 classSolution {2  Public:3     intRemoveelement (vector<int>& Nums,intval) {4         intLen =nums.size ();5         intPTR =0;6         intNewlen =0;7          for(inti =0; i<len;i++)8         {9             if(nums[i]!=val)Ten             { Onenums[ptr++]=Nums[i]; A             } -         } -Newlen =ptr; the         returnNewlen; -     } -};

Code two: Using STL

1 classSolution {2  Public:3     intRemoveelement (vector<int>& Nums,intval) {4Auto end = Remove (Nums.begin (), Nums.end (), Val);//The automatic pointer is used here.5         returndistance (Nums.begin (), end);6     }7};

[Leetcode] Remove Element Analysis

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.