[data structure brush question] array oj

前言:
	本文章是关于在力扣上面的数组相关面试题的讲解,包括:
	1.原地移除数组中所有的元素val,要求时间复杂度为O(N),空间复杂度为O(1),
	2.删除排序数组中的重复项。
	3. 合并两个有序数组。

1. Remove all elements val in the array in place

Title:
https://leetcode.cn/problems/remove-element/

1.1 The time complexity is O(N^2), and the space complexity is O(1)

Write a piece of code that removes all elements val in the array in place, requiring a time complexity of O(N^2) and a space complexity of O(1)
.
insert image description here

  int removeElement(int* nums, int numsSize, int val) {
    
    
    int i, j;
    int newSize = numsSize;

    for (i = 0; i < newSize; i++) {
    
    
        if (nums[i] == val) {
    
    
            for (j = i; j < newSize - 1; j++) {
    
    
                nums[j] = nums[j + 1];
            }
            newSize--;
            i--; // 重复检查当前位置
        }
    }

    return newSize; } 

int main() {
int nums[] = { 0, 1, 2, 2, 3, 0, 4, 2 };
int numsSize = sizeof(nums) / sizeof(nums[0]);
int val = 2;

int newSize = removeElement(nums, numsSize, val);

printf("修改后的数组: ");
for (int i = 0; i < newSize; i++) {
    printf("%d ", nums[i]);
}

return 0; } 

1.2 The time complexity is O(N), and the space complexity is O(N)

Write a piece of code that removes all elements val in the array in place, requiring a time complexity of O(N) and a space complexity of O(N): ideas
:
insert image description here

#include <stdlib.h> int* removeElements(int* nums, int numsSize, int val, int* newSize) {
       
       
     int i, j;
    		 int* tmp = (int*)malloc(numsSize * sizeof(int)); // 创建一个临时数组
     int count = 0; // 计数器,记录移除元素后的数组长度
   	  for (i = 0; i < numsSize; i++) {
     
     
         if (nums[i] != val) {
     
     
             tmp[count] = nums[i]; // 将非 val 元素保存到临时数组
             count++;
         }
     }
 
    *newSize = count; // 更新新数组的长度
 
    return tmp; // 返回临时数组的指针
     }

int main() {
     
     
    int nums[] = {
     
      0, 1, 2, 2, 3, 0, 4, 2 };
    int numsSize = sizeof(nums) / sizeof(nums[0]);
    int val = 2;

    int newSize;
    int* newArray = removeElements(nums, numsSize, val, &newSize);

    printf("移除元素后的数组: ");
    for (int i = 0; i < newSize; i++) {
     
     
        printf("%d ", newArray[i]);
    }
    printf("\n新长度: %d\n", newSize);

    free(newArray); // 释放临时数组的内存

    return 0; } ```

1.3 The correct way to write this question:

Remove all elements val in the array in place, requiring a time complexity of O(N) and a space complexity of O(1):

The only difference between writing method 2 and this article is that this article needs return j, and the rest is the same, and the general idea of ​​writing method 1 is not much different from drawing ideas, so there is no need for animation demonstration. Portal –> delete the specified number in the sequence , There are animation demonstrations Graphic
illustration:
insert image description here

#include<stdio.h>
/写法一
int removeElement(int *nums,int numsSize,int val)
{
    
    
	int src = 0;
	int dst = 0;
	while (src < numsSize)
	{
    
    
		if (nums[src] != val)
		{
    
    
			nums[dst++] = nums[src++];
		}
		else
		{
    
    
			src++;
		}
	}
	return dst;
}
//写法二
int removeElement(int* nums, int numsSize, int val){
    
    
      int i=0;
      int j=0;
      for(i=0;i<numsSize;i++)
      {
    
    
       if(nums[i]!=val)
      {
    
    
        nums[j++]=nums[i];
      }
      }
        return j;
}

int main() {
int nums[] = { 0, 1, 2, 2, 3, 0, 4, 2 };
int sz =sizeof(nums) / sizeof(nums[0]);
int val = 2;
int ret=removeElement(nums, sz, val);
printf(“%d”,ret);
}

2. Remove duplicates in the sorted array.

Title :
https://leetcode.cn/problems/remove-duplicates-from-sorted-array/

Animation demo:

insert image description here

#include<stdio.h>
int removeDuplicates(int* nums, int numsSize) {
    
    
	int src = 1, dst = 0;
	while (src < numsSize)
	{
    
    
		if (nums[src] != nums[dst])
		{
    
    
			nums[++dst] = nums[src++];
		}
		else
		{
    
    
			src++;
		}
	}
	return dst+1;//因为dst是下标,所以要+1
}

int main()
{
    
    
	int nums[] = {
    
     0,0,1,1,1,2,2,3,3,4 };
	int sz = sizeof(nums) / sizeof(nums[0]);
	int ret=removeDuplicates(nums, sz);
	printf("%d", ret);
}

implement:
insert image description here

3. Merge two sorted arrays

Title:
https://leetcode.cn/problems/merge-sorted-array/

Idea : No matter which of the following situations, the nums1 array is the target array, which is the long one.
If end2 ends first, you don’t need to copy to the target array nums1, just print nums1 directly.
If end1 ends first, you need to copy nums2 (elements of the short array) to nums1 first, and then print

3.1end1>0,end2<0

insert image description here

3.2end1<0,end2>0

insert image description here

Code:

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

Execution:
insert image description here
This is the end of the article, please correct me if there are any mistakes, thank you!

Guess you like

Origin blog.csdn.net/weixin_65186652/article/details/132095687