Data structure -- sequential table (array) topic

Posted by Valord on Sun, 09 Jan 2022 15:10:09 +0100

Array of related interview questions

Click to enter > > Remove element (LeetCode)

Give you an array num and a value val. you need to remove all elements with a value equal to Val in place and return the new length of the removed array.
Instead of using extra array space, you must use only O(1) extra space and modify the input array in place.
The order of elements can be changed. You don't need to consider the elements in the array that exceed the new length.

Source: LeetCode
Link: https://leetcode-cn.com/problems/remove-element
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Code example (C language)

int removeElement(int* nums, int numsSize, int val){
    int idx = 0;

        for(int i = 0; i < numsSize ;++i)
        {
            if(nums[i] != val)
            {
                nums[idx++] = nums[i];
            }
        }
        return idx;
}

Click to enter > > Delete duplicates in ordered array (LeetCode)

Give you an ordered array nums, please delete the repeated elements in place, make each element appear only once, and return the new length of the deleted array.
Do not use additional array space. You must modify the input array in place and complete it with O(1) additional space.

Source: LeetCode
Link: https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Code example (C language)

int removeDuplicates(int* nums, int numsSize){
    if(numsSize<=1)
         return numsSize;

    int idx = 1;
    for(int i = 1; i< numsSize; ++i)
    {
        if(nums[i] == nums[i-1])
            continue;
        else
        {
            nums[idx++] = nums[i];
        }

    }
    
    return idx;
}

Click to enter > > Merge two ordered arrays (LeetCode)

You are given two integer arrays nums1 and nums2 in non decreasing order, and two integers m and n representing the number of elements in nums1 and nums2 respectively.
Please merge nums2 into nums1 so that the merged array is also arranged in non decreasing order.

Source: LeetCode
Link: https://leetcode-cn.com/problems/merge-sorted-array
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Code example (C language)

void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
   int i = m - 1, j = n - 1, idx = nums1Size - 1;
   
   while(i>=0 && j>=0)
   {
       if(nums1[i]<nums2[j])
       {
           nums1[idx--] = nums2[j--];
       }
       else
       {
           nums1[idx--] = nums1[i--];
       }
   }
   if(j>=0)
   memcpy(nums1,nums2,sizeof(int)*(j+1));

Click to enter > > Rotation array (LeetCode)

Give you an array, rotate the elements in the array K positions to the right, where k is a non negative number.

Code example (C language)

void rotate(int* nums, int numsSize, int k){
    if(k >= 0)
    {
        k = ((numsSize>=k)?k:(k%numsSize));
        reverse(nums, 0, numsSize-k-1);
        reverse(nums, numsSize-k, numsSize-1);
        reverse(nums, 0, numsSize-1);

    } 

}
void reverse(int* nums, int begin, int end)
{
    while(begin<end)
    {
        nums[begin] = nums[begin]^nums[end];
        nums[end] = nums[begin]^nums[end];
        nums[begin++] = nums[begin]^nums[end--];
    }
}

Click to enter > > Integer addition in array form (LeetCode)

For the nonnegative integer x, the array form of X is an array of each number in left to right order. For example, if X = 1231, its array form is [1,2,3,1].
Given the array form A of nonnegative integer X, return the array form of integer X+K.

Source: LeetCode
Link: https://leetcode-cn.com/problems/add-to-array-form-of-integer
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Code example (C language)

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* addToArrayForm(int* num, int numSize, int k, int* returnSize){
    //Calculate the number of bits of integer k
    int len =0, kk = k;
    while(kk != 0)
    {
        len++;
        kk /= 10;
    }
    //Application array
    int arrlen = (numSize>len)?(numSize+1):(len+1);
    int* ret = (int*)malloc(sizeof(int)*arrlen);

    //carry
    int step = 0;
    int end = numSize -1;
    *returnSize = 0;

    while(arrlen-1 -(*returnSize) != 0 )
    {
        //Bitwise addition
        if(end>=0 && (num[end]+k%10 + step)>=10)
        {
            ret[arrlen-1 -(*returnSize) ] = (num[end]+k%10 + step) % 10;
            step = 1;
        }
        else if(end>=0 && (num[end]+k%10 + step)<10)
        {
            ret[arrlen-1- (*returnSize)] = (num[end]+k%10) + step;
            step = 0;
        }
        else
        {
            if((k%10 + step)>=10)
        {
            ret[arrlen-1 -(*returnSize) ] = (k%10 + step) % 10;
            step = 1;
        }
        else if((k%10 + step)<10)
        {
            ret[arrlen-1- (*returnSize)] = (k%10) + step;
            step = 0;
        }
        }

        if(k>0)
        k/=10;
        if(end>=0)
        end--;

        (*returnSize)++;
    }
   if(step == 1)
   {
       ret[0] = 1;
       (*returnSize)++;
   }

    if((*returnSize) == arrlen)
    return ret;
    else
    {
         int* ret1 = (int*)malloc(sizeof(int)*(*returnSize));
         memcpy(ret1, ret+1,sizeof(int)*(*returnSize) );
         return ret1;
    }



}

Topics: C Algorithm data structure leetcode