C programming question summary

Two numbers:

Given nums = [2, 7, 11, 15], target = 9

Because nums [0] + nums [1 ] = 2 + 7 = 9
is returned [0, 1]

int * twoSum ( int * the nums, int numsSize, int target, int * returnSize) {// Parameters: original array, the length of the original array, the comparison value, the returned array length
     int * RES = ( int *) the malloc ( the sizeof ( int ) * 2 );
     * returnSize = 0 ;
     int I, J;
     for (I = 0 ; I <numsSize- . 1 ; I ++ ) 
    { 
        for (J = I + . 1 ; J <numsSize; J ++) // turn back comparison 
        { 
            IF (the nums [I] + the nums [J] == target)  
            {
                RES [0]=i;
                res[1]=j;
                *returnSize=2;
                return res;
            }
        }
    }
    return res;
}

Duplicate elements:

Input: [1,2,3,1] Output: true

Input: [1,2,3,4] Output: false

void the swap ( int * A, int * B) have written the swap function // 
{ 
    int TEMP; 
    TEMP = * A;
     * = A * B;
     * B = TEMP; 
} 
void QUICKSORT ( int ARR [], int Start, int end) // merge sort have written 
{ 
    int arrBase, arrMiddle; 

    int tempStart = Start, 
        tempEnd = End; 

    // for conditions such recursive function, there must be an internal function returns 
    IF (tempStart> = tempEnd)
         return ; 

    //Copy of a reference value comparison as the parameters of the back 
    arrBase = ARR [Start];
     the while (Start < End) 
    { 
        the while (Start <End && ARR [End]> arrBase) 
            End - ;
         IF (Start < End) 
        { 
            the swap ( & ARR [Start], & ARR [End]); 
            Start ++ ; 
        } 

        the while (Start <End && ARR [Start] < arrBase) 
            Start ++ ;
         IF (Start < End) 
        { 
            the swap ( & ARR [Start], & ARR [ End]);
            End - ; 
        } 
    } 
    ARR [Start] = arrBase; 
    arrMiddle = Start; 

    // divide and conquer method of recursive 
    QUICKSORT (ARR, tempStart, arrMiddle- . 1 ); 
    QUICKSORT (ARR, arrMiddle + . 1 , tempEnd); 
} 
int CoMP ( const  void * A, const  void * B) {// write their own comparison function
     return (* ( int *) A> * ( int * ) B); 
} 
BOOL containsDuplicate ( int * the nums, intnumsSize) { 
    qsort (the nums, numsSize, the sizeof ( int ), CoMP); // values of the original array, sequentially sort
     for ( int K = 0 ; K <numsSize- . 1 ; K ++ ) {// judgment, the adjacent elements have equal, it returns true.
        IF (the nums [K] == the nums [K + . 1 ]) {
             return  to true ; 
        } 
    } 
    return  to false ; 
}

Two numbers together:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8

Reason: 342 + 465 = 807

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
//链表结构
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) 
{
    //初始化空头
    struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
    head->next =NULL;
    struct ListNode* tail;
    tail = head;
    struct* = P1 ListNode L1;
     struct ListNode * P2 = L2; 

    int with Carry = 0 ; // carry 
   IF (P1 == NULL) return P2; 
   IF (P2 == NULL) return P1;
// loop until two lists out // when two as long as this list only one process, but not as long, when, as long as the size of the process only the while (P1! = NULL && P2! = NULL) { // current node and, Note that together carry int SUM = P1-> P2- Val +> Val + with carry; // current node and not less than 10 IF (SUM> = 10 ) { SUM - = 10 ; //-10 current node value, becomes a bit with Carry = . 1 ; // greater than 10, the carry. 1 } the else { with Carry = 0 ; } // initialize node, the end of the addition, the tail pointer must be moved to a first end of a new and then assign the node, because they do not do so, the first node can not be assigned correctly (because this time the tail pointer has not really moved on the first node, this time pointing to the head or node) tail-> = Next ( struct ListNode *) the malloc ( the sizeof ( struct ListNode)); tail = tail-> Next; tail -> Val = SUM; P1 = P1-> Next; P2 = P2-> Next; } // When the list is not as long as two, one of which is NULL, and the other is not finished, by this time point p1 that a list poured, continue to traverse IF (p1 == NULL) { p1 = P2; } the else IF (P2 = NULL) { P1 = P1; } // traverse the remainder of the while (P1 =! NULL) { int SUM = P1-> Val + with Carry; // belt ary calculation and the current node IF (SUM> = 10 ) { SUM - = 10 ; with Carry = . 1 ; } the else { with Carry = 0 ; } // Add another toward the merged list node tail-> Next = ( struct ListNode *) the malloc ( the sizeof ( struct ListNode)); tail = tail-> Next; tail -> Val = SUM ; P1 = P1-> Next; } // if there is a last band, then apply a storage node. 1 IF (== with Carry . 1 ) { tail -> Next = ( struct ListNode *) the malloc ( the sizeof ( structListNode)); tail = tail-> Next; tail -> Val = . 1 ; } tail -> Next = NULL; // tail pointer nulling, end // because we can not return to the first node, so to release the head node , but to a head pointer to the first node struct ListNode * PTEMP = head; head = head-> Next; Free (PTEMP); return head; }

Integer reverse:

Input: 123 
Output: 321
Input: -123 
Output: -321
Input: 120 
Output: 21
int Reverse ( int X) 
{ 
    int TEMP;
     int I;
     long C = 0 ; // defined as a long type 
    for (I = 0 ; I < 10 ; ++ I) { 
        TEMP =% X 10 ; // remove the last bits include a negative case 
        X = X / 10 ; 
        C = C * 10 + TEMP;
         iF (C> 0x7FFFFFFF || C <(Signed int ) 0x80000000 ) { // determines whether the integer data overflow 
            return  0;
        }
        if(x==0) break;
    }
    return c;
}

 

Guess you like

Origin www.cnblogs.com/ming-4/p/11918733.html