Stay button (LeetCode) brush questions, simple questions (No. 5)

table of Contents

Problem 1: The number of binary 1

Problem 2: Print the maximum decimal number from 1 to n bits

Question 3: Delete list of nodes

Problem 4: adjust the order of the array so that in front of the even-odd

Question 5: linked list of the penultimate node K

Question 6: reverse list

Question 7: binary tree mirror

Question 8: Clockwise print matrix

Question 9: More than half of the number of times the array appears

Question 10: smallest number K


Stay button (LeetCode) regularly brush title, each lasting 10 questions, heavy traffic comrades can look at ideas I share, is not the most efficient solution, but only to enhance each other.

Problem 1: The number of binary 1

Questions requirements are as follows:

Answer (C language):

int hammingWeight(uint32_t n) {
    int cou=0;

    while(n>0){

        if(n%2==1){ //注意是二进制
            cou++;
        }

        n/=2;
    }

    return cou;
}

Operating efficiency as follows:


Problem 2: Print the maximum decimal number from 1 to n bits

Questions requirements are as follows:

Answer (C language):

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* printNumbers(int n, int* returnSize){
    int cou=(int)pow(10,n)-1;

    int* data_buf=(int*)malloc(sizeof(int)*(cou));

    for(int i=0;i<cou;i++)
        data_buf[i]=i+1;

    *returnSize=cou;
    
    return data_buf;
}

 Operating efficiency as follows:


Question 3: Delete list of nodes

Questions requirements are as follows:

Answer (C language):

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode* deleteNode(struct ListNode* head, int val){
    struct ListNode *p=head,*q=p->next;

    if(head->val==val) 
        return head->next;

    while(q){
        if(q->val==val){
            p->next=q->next;
            free(q);
            return head;
        }else{
            p=q;
            q=q->next;
        }
    }

    return head;
}

Operating efficiency as follows:


Problem 4: adjust the order of the array so that in front of the even-odd

Questions requirements are as follows:

Answer (C language):

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* exchange(int* nums, int numsSize, int* returnSize){
    int i=0,j=numsSize-1;
    int num=0;

    while(i<j){
        
        if(nums[i]%2==0 && nums[j]%2!=0){
            num=nums[i];
            nums[i]=nums[j];
            nums[j]=num;
        }

        if(nums[i]%2!=0)
            i++;

        if(nums[j]%2==0)
            j--;  
    }

    *returnSize=numsSize;
    return nums;
}

Operating efficiency as follows:


Question 5: linked list of the penultimate node K

Questions requirements are as follows:

Answer (C language):

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode* getKthFromEnd(struct ListNode* head, int k){
    struct ListNode* p=head,*q=p;
    int i=0;

    while(q!=NULL){
        q=q->next;
        i++;
    }

    i=i-k;

    while(i--){
       p=p->next; 
    }

    return p;
}

Operating efficiency as follows:


Question 6: reverse list

Questions requirements are as follows:

Answer (C language):

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode* reverseList(struct ListNode* head){
    struct ListNode *cur = NULL,*pre = head,*t;

    while (pre != NULL) {
        t = pre->next;
        pre->next = cur;
        cur = pre;
        pre = t;
    }
    
    return cur;
}

Operating efficiency as follows:


Question 7: binary tree mirror

Questions requirements are as follows:

Answer (C language):

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

struct TreeNode* mirrorTree(struct TreeNode* root){
    if (root==NULL) {
        return NULL;
    }

    struct TreeNode* right = mirrorTree(root->right);
    struct TreeNode* left = mirrorTree(root->left);

    root->left = right;
    root->right = left;
    
    return root;
}

Operating efficiency as follows:


Question 8: Clockwise print matrix

Questions requirements are as follows:

Answer (C language):

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize){
    if(matrixSize==0){
        *returnSize=0;
        return 0;
    }
    int cycle=0,row=0,column=0,k=0;
    *returnSize=matrixSize*(*matrixColSize);
    int *res=malloc(*returnSize * sizeof(int));
    while(k<*returnSize){
        res[k++]=matrix[row][column];
        if(row==cycle&&(column<*matrixColSize-cycle-1)) column++;
        else if((column==*matrixColSize-cycle-1)&&(row<matrixSize-cycle-1)) row++;
        else if((row==matrixSize-cycle-1)&&column>cycle) column--;
        else if(column==cycle&&(row>cycle+1)) row--;
        else{
            cycle++;
            column++;
        }
    }
    return res;
}

Operating efficiency as follows:


Question 9: More than half of the number of times the array appears

Questions requirements are as follows:

Answer (C language):

int majorityElement(int* nums, int numsSize){
    int key = nums[0];
    int count = 0;
    
    for (int i = 0; i < numsSize; i++)
    {
        if(nums[i] == key)
            count++;
        else
            count--;
        
        if(count <= 0)
        {
            key = nums[i+1];
        }
        
    }
    return key;
}

Operating efficiency as follows:


Question 10: smallest number K

Questions requirements are as follows:

Answer (C language):

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* getLeastNumbers(int* arr, int arrSize, int k, int* returnSize){
    int num=0;
    
    for(int i = 0; i < arrSize - 1; i++)
    {
        for(int j = i+1; j < arrSize; j++)
        {
            if(arr[i] > arr[j])
            {
                num = arr[i];
                arr[i] = arr[j];
                arr[j] = num;
            }
        }
    }

    returnSize[0]=k;

    return arr;
}

The efficiency is shown below (simple logic, efficiency terrible!):


Published 156 original articles · won praise 8297 · Views 830,000 +

Guess you like

Origin blog.csdn.net/m0_38106923/article/details/104471474