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.