LeetCode Brush Questions, Simple Questions (Issue 24)

table of Contents

Question 1: The minimum index sum of the two lists

Question 2: Reverse vowels in a string

Question 3: Inverting integers

Question 4: Convert an ordered array to a binary search tree

Question 5: The Nth Tebonacci number

Question 6: Array serial number conversion

Question 7: Sorting Prime Numbers

Question 8: A few days between dates

Question 9:-Day of the year

Question 10: Copy Zero


LeetCode regularly brushes questions, 10 questions per issue. Comrades with heavy business can look at the ideas I shared. It is not the most efficient solution, but only seeks to improve each other.

Question 1: The minimum index sum of the two lists

The test questions are as follows:

 

Answer ideas:

Hey, brute force.

Answer (C language):

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
char ** findRestaurant(char ** list1, int list1Size, char ** list2, int list2Size, int* returnSize){
    int minNum = list1Size > list2Size ? list2Size : list1Size;
    char **res = (char **)malloc(sizeof(char *) * minNum);
    int index = INT_MAX;
    int num = 0;

    for (int i = 0; i < list1Size; i++){
        for (int j = 0; j < list2Size; j++) {
            if (strcmp(list1[i], list2[j]) == 0){
                if (i + j < index){
                    index = i + j;
                    res[0] = list1[i];
                    num = 1;
                }
                else if (i + j == index){
                    res[num] = list1[i];
                    num++;
                }
            }
        }
    }

    *returnSize = num;
    return res;
}

The operating efficiency is as follows:


Question 2: Reverse vowels in a string

The test questions are as follows:

Answer ideas:

元音 字母 : a [ei] 、 e [i:] 、 i [ai] 、 o [eu] 、 u [ju:]。

Answer (C language):

bool isAEIOU(char c){
    return (c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U');
}

char * reverseVowels(char * s){
    int len = strlen(s);
    if(len<2) return s;
    
    int head=0, tail=len-1;

    while(tail>head){
        if(isAEIOU(s[head])&&isAEIOU(s[tail])){
            char temp;
            temp = s[head];
            s[head] = s[tail];
            s[tail] = temp;
            head++, tail--;
        }
        else{
            if(!isAEIOU(s[head]))head++;
            if(!isAEIOU(s[tail]))tail--;
        }
    } 
    
    return s;
}

The operating efficiency is as follows:


Question 3: Inverting integers

The test questions are as follows:

Answer (C language):

#define isOverLength 0

int reverse(int x){
    long lRet = 0;
    while(0 != x){
        lRet = lRet * 10 + x % 10;
        x = x / 10;
    }

    if((int)lRet != lRet){
        return isOverLength;
    }

    return (int)lRet;
}

The operating efficiency is as follows:


Question 4: Convert an ordered array to a binary search tree

The test questions are as follows:

Answer ideas:

Recursive method: locate the root node, the left side of the root node is treated as the left branch recursively, and the right side of the root node is treated as the right branch recursively.

Answer (C language):

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

struct TreeNode* sortedArrayToBST(int* nums, int numsSize){
    int     iRoot       = 0;
    struct TreeNode*    pCurNode    = NULL;

    //1,结束条件
    if((NULL == nums) || (0 == numsSize)) return NULL;

    //2,初始化
    pCurNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));

    //3,定位根节点
    iRoot = numsSize / 2;

    //4,递归处理左右支
    pCurNode->val = nums[iRoot];
    pCurNode->left = sortedArrayToBST(&nums[0], iRoot);
    pCurNode->right = sortedArrayToBST(&nums[iRoot + 1], numsSize - iRoot - 1);
    return pCurNode;
}

The operating efficiency is as follows:


Question 5: The Nth Tebonacci number

The test questions are as follows:

Answer (C language):

int tribonacci(int n){
    if(n==0) return 0;
    if(n==1||n==2) return 1;

    int dp[n+1];
    
    dp[0]=0;
    dp[1]=1;
    dp[2]=1;

    for(int i=3;i<=n;i++){
        dp[i]=dp[i-1]+dp[i-2]+dp[i-3];
    }
    
    return dp[n];
}

The operating efficiency is as follows:


Question 6: Array serial number conversion

The test questions are as follows:

Answer ideas:

First use a two-dimensional array to store the values ​​and subscripts in arr, and then sort the first dimension of this two-dimensional array, from front to back, directly modify the values ​​corresponding to the stored subscripts.

Answer (C language):

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int cmp(const void *a, const void *b)
{
    return ((int *) a)[0] - ((int *) b)[0];
}

int *arrayRankTransform(int *arr, int arrSize, int *returnSize)
{
    *returnSize = arrSize;

    if (arrSize == 0)
    {
        return arr;
    } else if (arrSize == 1)
    {
        arr[0] = 1;
        return arr;
    }
    int dis[arrSize][2];
    int i, k;

    for (i = 0; i < arrSize; i++)
    {
        dis[i][0] = arr[i];
        dis[i][1] = i;
    }

    qsort(dis, arrSize, sizeof(dis[0]), cmp);

    k = 1;
    arr[dis[0][1]] = 1;
    for (i = 1; i < arrSize; i++)
    {
        if (dis[i][0] > dis[i - 1][0])
        {
            k++;

        }
        arr[dis[i][1]] = k;
    }
    return arr;
}

The operating efficiency is as follows:


Question 7: Sorting Prime Numbers

The test questions are as follows:

Answer ideas:

There are m prime numbers and n non-prime numbers. The result is m!*n!.

Note: the calculation of factorial int type will overflow with long int; do not use scientific notation.

Answer (C language):

const long long int m=1000000007;

int prime(int n){
    if(n==1)return 0;

    for(int i=2;i<(n/2)+1;i++)
        if(n%i==0)return 0;

    return 1;
}
int numPrimeArrangements(int n){
        int i,p=0,q;
        long long int jq=1,jp=1;

        for(i=1;i<=n;i++) if(prime(i)==1)p++;//i是质数

        q=n-p;

        for(i=1;i<=q;i++) jq=(jq*i)%m;
        for(i=1;i<=p;i++)jp=(jp*i)%m;

        return (jp*jq)%m;
}

The operating efficiency is as follows:


Question 8: A few days between dates

The test questions are as follows:

Answer (C language):

int isleap(int y){
    return y%4==0 && y%100!=0 || y%400==0;
}

int tab[]={-1,31,28,31,30,31,30,31,31,30,31,30,31};

int getdate(char *date){ 
    int y,m,d,r=0;
    sscanf(date, "%d-%d-%d", &y,&m,&d);
    for(int i=1970; i<y;i++)
        if(isleap(i)) r+= 366;
        else r+= 365;
    for(int i=1;i<m;i++){
        r+=tab[i];
        if(i==2 && isleap(y)) r+=1;
    }
    r+=d;
    
    return r;
}

#define intfabs(x) ((x)<0?-(x):(x))

int daysBetweenDates(char * date1, char * date2){
    return intfabs(getdate(date1)-getdate(date2) );
}

The operating efficiency is as follows:


Question 9: -Day of the year

The test questions are as follows:

Answer (C language):

int dayOfYear(char * date){
    int year=0,month=0,day=0,result=0,i;
    int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};

    for(i=0;i<4;i++) year=year*10+date[i]-48;
    for(i=5;i<7;i++) month=month*10+date[i]-48;
    for(i=8;i<10;i++) day=day*10+date[i]-48;

    if(year%4==0&&year%100!=0||year%400==0) days[1]=29;
    for(i=0;i<month-1;i++) result=result+days[i];
    result=result+day;
    
    return result;
}

The operating efficiency is as follows:


Question 10: Copy Zero

The test questions are as follows:

Answer (C language):

void duplicateZeros(int* arr, int arrSize){
     for(int i = 0;i < arrSize;i++){
         if(arr[i] == 0){
            for(int j = arrSize-2;j >= i;j--)
                arr[j+1] = arr[j];
            i++;
         }
     }  
}

The operating efficiency is as follows:

Guess you like

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