directory

Problem 1: Word rules

Problem 2: Find the difference

Problem 3: Find the first and last position of an element in a sorted array

Problem 4: Climb stairs at minimum cost

Problem 5: Looking for peaks

Problem 6: The first unique character in a string

Problem 7: Intersection of two arrays II

Problem 8: Distributing cookies

Problem 9: Rotate the image

Problem 10: set the matrix to zero


LeetCode brushes the questions regularly, with 10 questions in each period. Comrades with heavy business can look at the ideas I share, which are not the most efficient solutions, but for mutual improvement.

Question 1:The word law

The requirements are as follows:

Answer (C language) :

bool wordPattern(char * pattern, char * str){
    char **hash = (char* *)malloc(26 * sizeof(char*));

    for (int i = 0; i < 26; ++i)
    {
        hash[i] = (char*)malloc(64 * sizeof(char));
        memset(hash[i], 0.64 * sizeof(char));
    }
    
    int len = strlen(pattern);

    for (int i = 0; i < len; ++i)
    {
        char *p = str;
        while(p && *p ! =0&& *p ! =' ') ++p;
        if (' ' == *p) *p++ = 0;
        if (strlen(str) == 0)
            return false;
        int pos = pattern[i] - 'a';
        if (strlen(hash[pos]) == 0)
        {
            for (int j = 0; j < 26; ++j)
            {
                if(j ! = pos &&strlen(hash[j]) > 0)
                {
                    if (strcmp(hash[j], str) == 0)
                        return false; }}strcpy(hash[pos], str);
        }
        else
        {
            if (strcmp(hash[pos], str) ! =0)
                return false;
        }
        str = p;        
    }

    if (strlen(str) > 0)
        return false;
        
    return true;
}
Copy the code

The operating efficiency is as follows:


Question 2:Looking for different

The requirements are as follows:

Answer:

Answer (C language) :

char findTheDifference(char* s, char* t) {
    int n = strlen(s), m = strlen(t);
    int as = 0, at = 0;

    for (int i = 0; i < n; i++) {
        as += s[i];
    }
    for (int i = 0; i < m; i++) {
        at += t[i];
    }
    
    return at - as;
}
Copy the code

The operating efficiency is as follows:


Question 3:Finds the first and last position of an element in a sorted array

The requirements are as follows:

Answer (C language) :

int* searchRange(int* nums, int numsSize, int target, int* returnSize){
    int *ret=(int *)malloc(sizeof(int) *2);
    ret[0] =- 1;
    ret[1] =- 1;
    *returnSize=2;
    
    if(numsSize==0||target<nums[0]||target>nums[numsSize- 1]) {return ret;
    }
    int left=0, right=numsSize- 1, mid=0, head=0, tail=0;
    while(left<=right){
        mid=(left+right)/2;
        if(nums[mid]>=target){
            right=mid- 1;
        }
        else{
            left=mid+1;         //mid is the index of the first value >=target
        }
    }

    head=left;
    left=0, right=numsSize- 1, mid=0;

    while(left<=right){
        mid=(left+right)/2;
        if(nums[mid]<=target){
            left=mid+1;
        }
        else{
            right=mid- 1;
        }
    }
    tail=right;
    if(nums[head]==target){     // This number exists
        ret[0]=head;
        ret[1]=tail;
    }

    return ret;
}
Copy the code

The operating efficiency is as follows:


Question 4:Use stairs at minimal cost

The requirements are as follows:

Answer (C language) :

int minCostClimbingStairs(int* cost, int costSize) {
    int prev = 0, curr = 0;

    for (int i = 2; i <= costSize; i++) {
        int next = fmin(curr + cost[i - 1], prev + cost[i - 2]);
        prev = curr;
        curr = next;
    }
    
    return curr;
}
Copy the code

The operating efficiency is as follows:


Question 5:Looking for peak

The requirements are as follows:

Answer (C language) :

int findPeakElement(int *nums, int numsSize)
{
    if(! nums || numsSize <1) {
        return 0;
    }
    if (numsSize == 1) {
        return 0;
    }
    if (nums[0] > nums[1]) {
        return 0;
    }
    if (nums[numsSize - 1] > nums[numsSize - 2]) {
        return numsSize - 1;
    }
    for (int i = 1; i < numsSize - 1; i++) {
        if (nums[i] > nums[i - 1] && nums[i] > nums[i + 1]) {
            returni; }}return - 1;
}
Copy the code

The operating efficiency is as follows:


Question 6:The first unique character in a string

The requirements are as follows:

Answer:

Traverse twice, once record the number of characters, once find the first occurrence of the index.

Answer (C language) :

int firstUniqChar(char * s){
    if(s == NULL || strlen(s) == 0) return - 1;
    int len = strlen(s);
    if(len == 1) return 0;
    int recode[26] = {0};

    // Count the number of characters
    for(int i=0; i<len; i++){ recode[s[i]-'a'] + +; }// Find the first occurrence of the index
    for(int i=0; i<len; i++){if(recode[s[i]-'a'] = =1) {returni; }}return - 1;
}
Copy the code

The operating efficiency is as follows:


Question 7:Intersection of two arrays II

The requirements are as follows:

Answer:

Answer (C language) :

The operating efficiency is as follows:


Question 8:Distribution of biscuits

The requirements are as follows:

 

Answer (C language) :

int compare(const void * a, const void * b)
{
    return ( *(int*)b - *(int*)a );
}

int findContentChildren(int* g, int gSize, int* s, int sSize){
    int count=0;

    qsort (g, gSize, sizeof(int), compare);
    qsort (s, sSize, sizeof(int), compare);
    
    for(int i = 0, j = 0; i < gSize && j < sSize; i++, j++)
    {
        if(s[j] >= g[i])
            count++;
        else
            j--;
    }

    return count;
}
Copy the code

The operating efficiency is as follows:


Question 9:Rotate the image

The requirements are as follows:

Answer (C language) :

void rotate(int** matrix, int matrixSize, int* matrixColSize) {
    int matrix_new[matrixSize][matrixSize];

    for (int i = 0; i < matrixSize; i++) {
        for (int j = 0; j < matrixSize; j++) { matrix_new[i][j] = matrix[i][j]; }}for (int i = 0; i < matrixSize; ++i) {
        for (int j = 0; j < matrixSize; ++j) {
            matrix[j][matrixSize - i - 1] = matrix_new[i][j]; }}}Copy the code

The operating efficiency is as follows:


Question 10:Matrix zero

The requirements are as follows:

Answer (C language) :

void setZeroes(int** matrix, int matrixSize, int* matrixColSize){
    int i = 0;
    int j = 0;
    int iRow = matrixSize;
    int iCol = matrixColSize[0];
    int row[iRow];
    int col[iCol];

    memset(row, 0x00.sizeof(int) * iRow);
    memset(col, 0x00.sizeof(int) * iCol);

    //1, find all elements 0, and mark in the row and column array
    for (i = 0; i < iRow; i++)
    {
        for (j = 0; j < iCol; j++)
        {
            if (matrix[i][j] == 0)
            {
                row[i] = 1;
                col[j] = 1; }}}//2, iterate over the number group, modify the matrix according to the result in the row and column
    for (i = 0; i < iRow; i++)
    {
        for (j = 0; j < iCol; j++)
        {
            if (row[i] == 1)
            {
                matrix[i][j] = 0;
            }
            if (col[j] == 1)
            {
                matrix[i][j] = 0; }}}}Copy the code

The operating efficiency is as follows: