Necessary classic algorithm questions for interviews (c language implementation)

Table of contents

Preface: This chapter will introduce three classic topics from leetcode, the main idea involved: double pointer  

1: The sword refers to Offer 58 - II. Rotate the string to the left - LeetCode

2: 977. Squaring an Ordered Array - LeetCode                                                                                    

3:27. Removing elements - LeetCode                                                                                                             

                               


          Title description:

        Given a string, and an integer n asks us to rotate the first n characters of the string to the end of the string.

        

Let me introduce the solution to this problem: -->

        Solution 1: violent solution time complexity is o(n^2)  

        Idea: We first write the code that rotates 1 character to the left, and then execute it n times.

                The idea of ​​​​left-rotating a character: We first save the first element with the tmp variable, then move the elements behind the first element to the front, and then put tmp in the last position.

       picture:

        code:

        

  while(n--)
    {
        char tmp=*s;
        int i =1;
        for(i=1;i<strlen(s);i++)
        {
            s[i-1]=s[i];//挪动元素
        }
        //最后一个字符改为原来的第一个
        s[i-1]=tmp;
    }

But the brute force solution ran in leetcode because the time complexity was too high.

        So we have to change the solution.

Solution 2: 3-step reverse string method time complexity is O(N) space O(1)

        Idea: We first reverse the first n characters in the string, then reverse the strings after n, and finally reverse the overall order. Modify a string in-place.

        When writing code, we have to pay attention to the maximum string subscript n-1.

code:

void reverse(char** pps,int left,int right)
{
    while(left<right)
    {
        char tmp=(*pps)[left];
        (*pps)[left]=(*pps)[right];
        (*pps)[right]=tmp;
        left++;
        right--;
    }
}

char* reverseLeftWords(char* s, int n){
    reverse(&s,0,n-1);//逆置前n个
    reverse(&s,n,strlen(s)-1);//逆置n后面
    reverse(&s,0,strlen(s)-1);//整体逆置
    return s;
}

Solution 3 : Cutting + Merging Time O(n) Space O(n)

        Idea: Take out the first n characters, add a '\0' character, store the remaining characters, and finally combine the two characters, put the last n characters in front, and splice the first n characters in the back, will be able to achieve.

        code:

char* reverseLeftWords(char* s, int n){
    n=n%(strlen(s)+1);
    char* ret1=(char*)malloc(sizeof(char)*(n+1));//放要旋转的字符还得放'\0'
    char* ret2=(char*)malloc(sizeof(char)*(strlen(s)+1));
    //先放要旋转的字符串
    int i=0;
    int j=0;
    for(i=0;i<n;i++)
    {
        ret1[j]=s[i];
        j++;
    }
    ret1[j]='\0';
    j=0;
    for(i=n;i<strlen(s);i++)
    {
        ret2[j]=s[i];
        j++;
    }
    int k =0;
    while(ret1[k]!='\0')
    {
        ret2[j++]=ret1[k++];

    }
    ret2[j]='\0';
  
    return ret2;

}

Summary: Among these three methods, the second method is undoubtedly the best, but it is also the most difficult to think of.

        After sharing this question, let's move on to the next question. 

    

2:  977. Squaring an ordered array - LeetCode                                                                                                                                                                                                method 1: sorting plus traversal                                                                                                                      
Idea: We first square all the elements in the array, and then add a quick sort to solve it. Time complexity: O(nlogn) code implementation:
 //时间复杂度O(n),空间复杂度O(n),双指针
int* sortedSquares(int* nums, int numsSize, int* returnSize){
    int left=0;
    int right=numsSize-1;
    while(left<=right)
    {  
        if(nums[left]*nums[left]>nums[right]*nums[right])
        {
            int tmp=nums[right];
            nums[right--]=nums[left]*nums[left];
            nums[left]=tmp;
        }
        else
        {
           nums[right--]=nums[right]*nums[right];
        }
    }
    * returnSize=numsSize;
    return nums;
}
picture:                                                                                                                                                     
      
 Method 2:   Because we know that the array is an ordered array, the value of the square increases as we go to both sides, so we use the double pointer method to solve the problem and put the larger element at the end.
picture:
  code:
 //时间复杂度O(n),空间复杂度O(n),双指针
int* sortedSquares(int* nums, int numsSize, int* returnSize){
    //新开辟的一块空间,
    int* ret=(int*)malloc(sizeof(int)*numsSize);
    int i =numsSize-1;
    int left=0;
    int right=numsSize-1;
    while(left<=right)
    {
        if(nums[left]<0)
            nums[left]=-nums[left];
        if(nums[left]>nums[right])
        {
            ret[i--]=nums[left]*nums[left];
            left++;            
        }
        else
        {
             ret[i--]=nums[right]*nums[right];
            right--;           
        }
    }
    * returnSize=numsSize;
    return ret;
}
This question is shared -_-

3:27 . Removing Elements - LeetCode                                                                                                     

 

   Remove the same elements: We will directly explain the double-pointer method for this question, and we will not talk about the violent solution.

        Double pointer method: We use two pointers, one pointer is used to traverse the elements of the entire array, and the other pointer is used to modify the elements in the array.

 

        code:

int removeElement(int* nums, int numsSize, int val){
    int fast=0;
    int slow=0;
    while(fast<numsSize)
    {
        if(nums[fast]!=val)
        {
            nums[slow]=nums[fast];
            slow++;
        }
        fast++;
    }
    return slow;
}

 The classic algorithm questions in this chapter are over, if you think it is useful to you, you can give it a thumbs up!

        If there is anything wrong, welcome to point out.

        

                               

Guess you like

Origin blog.csdn.net/2201_75964502/article/details/132637062
Recommended