Leetcode: remove_element, leetcode

Source: Internet
Author: User

Leetcode: remove_element, leetcode

I. Question

Given an array and a value, delete the elements equal to the given value. Returns the length of the new array.

Ii. Analysis

At the beginning, I thought I only needed to return the final array length! After WA, we realized that we had to construct the array of hearts. Therefore, we can scan the array to save unequal values and delete equal values. The elements scanned will either be saved or discarded. In this case, we can use the tar flag to record them.

 

class Solution {public:    int removeElement(int A[], int n, int elem) {    int tar;         for(int i=0;i<n;i++) {        if(elem!=A[i]) {        A[tar]=A[i];        tar++;        }        }        return tar;    }};



How to program leetcode

Step 1: Enter www.leetcode.com in the browser
Step 2: click Register in the upper right corner.
Step 3: Register and log on, and click Online judge in the upper right corner.
Step 4: You may be logged on again. Click sing in instead of sign up and enter the user name and password to log on:
Step 5: select a question, select a language, enter the code in the code editor, and submit solution. The system will judge whether the result is correct or not. All submitted code systems will be saved for you, which can be viewed in my submission.
 
C ++ leetcode always says that the compilation is wrong. The key is that I don't even have 77th lines, only those lines ......

Neither leetcode can define the main function.
You need to construct a Solution class

The main of the 77 rows of redefine happens to be the main function of leetcode used to test the Solution you write.

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.