Summary of array-related interview questions

This is about some questions about arrays on LeetCode

1. Remove all elements val in the array in place.
Insert picture description here
Idea: double pointer traversal, idx and i, i is not equal to val, move to the next bit to the right, because the problem requires that memory cannot be applied for, although the code after watching applies for memory, but Likou compiler has been compiled

int removeElement(int* nums, int numsSize, int val)
{
    
    
//int* newA=(int*)malloc(numsSize*sizeof(int));
int idx=0;
for(i=0;i<numsSize;++i)
{
    
    
if(nums[i]!=val)
nums[idx++]=nums[i];
}
//memcpy(nums,newA,sizeof(int)*idx);
//free(newA);
return idx;
}

2. Delete duplicates in the sorted array. The
Insert picture description here
idea of this question is still double pointers, O(n) complexity, and no extra array space is used

int removeDuplicates(int* nums, int numsSize)
{
    
    
int idx=0;
int i=0;
int j=0;
while(j<numsSize)
{
    
    
   nums[idx++]=nums[i];
   if(nums[i]!=nums[j])
   {
    
    
       ++i;
       ++j;
   }
   else
   {
    
    
       //找到下一个不同元素位置
       while(j<numsSize&&nums[i]==nums[j])
       ++j;
       i=j;
       ++j;
   }
}
if(i<numsSize)
nums[idx++]=nums[i];
return idx;
}

3. Merge two ordered arrays
Insert picture description here

void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n)
{
    
    
//从后向前合并
int idx=m+n-1;
while(m>0&&n>0)
{
    
    
    if(nums1[m-1]>=nums2[n-1])
    {
    
    
        nums1[idx--]=nums1[m-1];
        --m;
    }
    else
    {
    
    
        nums1[idx--]=nums2[n-1];
        --n;
    }
    }
    if(n>0)
    {
    
    
        memcpy(nums1,nums2,sizeof(int)*n);
    }
}

4. Rotate the array (two solutions: left-handed and right-handed)
Insert picture description here
solution one:

void reverse(int* nums,int left,int right)
{
    
    
    while(left<right)
    {
    
    
        int tmp=nums[left];
        nums[left]=nums[right];
        nums[right]=tmp;
        ++left;
        --right;
    }
}
void rotate(int* nums, int numsSize, int k)
{
    
    
    k%=numsSize;
    //右旋
    reverse(nums,0,numsSize-1);
    reverse(nums,0,k-1);
    reverse(nums,k,numsSize-1);
}

Solution two:

void reverse(int* nums,int left,int right)
{
    
    
    while(left<right)
    {
    
    
        int tmp=nums[left];
        nums[left]=nums[right];
        nums[right]=tmp;
        ++left;
        --right;
    }
}
void rotate(int* nums, int numsSize, int k)
{
    
    
   //左旋
    k%=numsSize;
     reverse(nums,0,numsSize-k-1);
     reverse(nums,numsSize-k,numsSize-1);
     reverse(nums,0,numsSize-1);
 }
 //左旋两次相当于右旋五次

5. Integer addition in the form of an array. I have
Insert picture description here
been tortured by this question for a long time.

First determine how
many digits of the added integers are. Add two numbers and store them in the newly created array.
Directly invert the array. The
key is to take the carry into account when adding.

Insert picture description here

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

 }
int* addToArrayForm(int* A, int ASize, int K, int* returnSize)
{
    
    
//把数字的数组形式转化成整数
//整数相加
//整数转成数组形式
//上面的不可行 如果碰上大数 

//获取最大位数
int tmp=K;
int knum=0;
while(tmp)
{
    
    
    knum++;
    tmp/=10;
}
//开空间考虑最高进位
int newArraySize = ASize > knum ? ASize+1 : knum+1;
int* newArray=(int*)malloc(sizeof(int)*newArraySize);
//模拟加法运算,从低位开始相加
int i=0;
int idx=ASize-1;
int step=0;
while(idx>=0||K>0)
{
    
    
    newArray[i] = step;
    if(idx>=0)
    newArray[i]+=A[idx];
    if(K>0)
    newArray[i]+=K%10;
    //更新进位
    if(newArray[i]>9)
    {
    
    
        newArray[i]-=10;
        step=1;
    }
    else
    step=0;
    //继续下一位的加法运算
    --idx;
    K /= 10;
    i++;
}
//判断最高位是否有进位
if(step==1)
   newArray[i++]=1;

   //逆转
   reverse(newArray,0,i-1);
   *returnSize = i;
   return newArray;
}

Guess you like

Origin blog.csdn.net/qq_45657288/article/details/109107140