Leetcode (2) Remove duplicates from Sorted Array

Source: Internet
Author: User
Tags constant modifier
Remove duplicates from Sorted Array

An array of topic studies
Describe
Given a sorted array, remove the duplicates in place such, all element appear only
Once and return the new length.
Do the allocate extra space for another array, and you must does this on place with constant memory.
For example, Given input array A = [1,1,2],
Your function should return length = 2, and A is now [all].

C + + code is as follows
Time complexity O (n), Spatial complexity O (1):

/
* * Remove duplicates from Sorted array
* For example Given input array a= [1,1,2] 
* Your function should ret Urn length = 2; and A is now [all]
*/

#include <iostream>
using namespace std;


Class solution{public


:
    //return value, function parameter, implementation function
    //pointer to an array of shapes, array and number of arrays, implementing the array element de-weight
    int * removeduplicates (int *a,int num) {

        //idea: According to judge whether the adjacent two elements are the same, if not the same, the latter element overwrites the previous element
        //      index record the number of new array
        int index =0;

        Starting from the second element compare
        for (int i=1;i<num;i++) {
            //if the adjacent two elements are different if
            (A[index]!=a[i]) {
                index++;
                A[index]=a[i];
            }

        }

        for (int j=0;j<=index;j++) {
            cout<<a[j]<< "";
        }
        cout<<endl;


        return A;
    }


;


int main () {

    int a[]={1,1,2,2,3};

    Solution S1;
    S1. RemoveDuplicates (a,5);


    GetChar ();
    GetChar ();
    return 0;
}
Result
1 2 3
Conceptual Understanding Const Definition UnderstandingConst retouching
The case of the const modifier pointer is generally divided into the following four scenarios:
int b =500;
Const int* A = &b; Case 1
int const *A = &b;//Case 2
int * Const a=&b;//Condition 3
const int* const a= &b;//Case 4

How to differentiate it.
(1) Situation 1
If the const is on the left side of the asterisk, the const is used to decorate the variable that the pointer points to, that is, the pointer points to a constant. If the const is on the right side of the asterisk, const is the modifier pointer itself, that is, the pointer itself is a constant. Therefore, in case 1 and Case 2 are the same, the content pointed to by the pointer is constant (the const is placed regardless of the position of the variable declarator), in which case the content is not allowed to be changed.

To cite a popular example, if A is a warehouse administrator, he enters the warehouse where the goods (*a) are not authorized to move. What is the difference between a const and a # define.

Answer

The C + + language can define constants with const, or you can define constants in # #, but the former has more advantages than the latter:

1, const constants have data types, and macro constants do not have data types. The compiler can perform type safety checks on the former, with only character substitution, no type security checks, and unexpected errors, or marginal effects, that can occur during string substitution.

2. In some integrated debugging tools, const constants can be debugged, but macro constants cannot be debugged. In C + + programs, only const constants are used instead of macro constants, that is, const constants completely supersede macro definitions.

The introduction of the

Constants was in earlier versions of C + +, when the standard C specification was being customized. At that time, constants were considered a good idea and were included in C. However, the Const in C means "an ordinary variable that cannot be changed." In C, it always takes up memory, and its name is the global character. The C compiler cannot see Const as a constant during compilation.

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.