Array OJ question

Topic One

(1) Description

Given an array of nums and a value of val, you need to remove all elements whose value is equal to val in place, and return the new length of the removed array.

Don't use extra array space, you must use only O(1) extra space and modify the input array in-situ. The order of the elements can be changed. You don't need to consider the elements in the array beyond the new length.

(2) Example

输入nums=[3,2,2,3],val=3
输出nums=[2,2],数组长度位2

(3) Thinking

Double pointer method

  1. First define a slow pointer slow, and stipulate that the value of the 0-slow element is not val

Insert picture description here
2. Then use the fast pointer to traverse the array Insert picture description here
and compare the element pointed to by fast with val, there will be two situations: the value of the element pointed to is equal to or not equal to val fast
3. If the value of the element pointed to is equal to val (as shown above ), because we stipulate that o-slow is to store an element
Insert picture description here
whose value is not val, the fast pointer is moved backward, looking for the next element that came to "2" at this time, and found that its value is not val, then put it in In the 0-slow interval, that is, at this time, assign the element pointed to by fast to slow. After the
Insert picture description here
assignment is completed, the slow pointer must move backward Insert picture description here
. 4. Repeat the above steps

(4) Code implementation

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

Insert picture description here

Topic two

(1) Description

Given a sorted array, you need to delete the repeated elements in place, so that each element only appears once, return the new length of the removed array.
Do not use extra array space, you must modify the input array in place and use O (1) Complete with additional space.

(2) Examples

输入nums=[1,1,1,2,2,2,3]
输出nums=[1,2,3]
返回数组长度为3

(3) Thinking

This question is a bit similar to the first question, see the figure below for specific ideas
Insert picture description here

Insert picture description here

(4) Code

int removeDuplicates(int* nums, int numsSize)
{
    
    
    int front=0;
    int behind=0;
    if(numsSize==0)//特别注意空数组的情况,往往就是这一个测试用例无法通过
    {
    
    
        return 0;
    }
    else
    {
    
    
        for(behind=1;behind<numsSize;behind++)
        {
    
    
            if(nums[front]!=nums[behind])
            {
    
    
                front++;
                nums[front]=nums[behind];
            }
        }
        return front+1;
        //注意这里要返回front+1,因为测试在输出时在到front时会停止
    }
}

Insert picture description here

Topic Three

(1) Description

For non-negative integer X, the array form of X is an array formed by each digit from left to right. For example, if X = 1231, then its array form is [1,2,3,1].

Given the array form A of non-negative integer X, return the array form of integer X+K.

(2) Example

Insert picture description here

(3) Thinking

For example, X=1200, its array form A=[1,2,0,0], if K is 34, then X+K=1234, X+K array form=[1,2,3,4].
So start from the ones place and add them one by one. After adding one, put them in the array. Since they are put in order, the array must be reversed at the end.

First, we ask for the number of K to determine how big the array is

int* addToArrayForm(int* A,int ASize,int K,int* returnSize)
{
    
    
	int KSize=0;
	int KNum=k;
	while(KNum)
	{
    
    
		++KSize;
		KNum/=10;
	}
}
int len=ASize>KSize?ASize:KSize;
int* retarr=(int*)malloc(sizeof(int)*(len+1));//找出这两个数组哪个大,新的数组最厉害也只能比它大一位

The next step is to add up one by one starting from the ones place. The problem of carry will be involved when adding.

int Ai=ASize-1;//找到数组A的最后一位
int reti=0;//reti用于控制相加后的下标
int nexnum=0;//用于控制进位
while(len--)//比如说最大长度为4为,那么他就要进行4次运算
{
    
    
	int a=0;
	if(Ai>=0)//如果是1200+34那都没有问题,因为Ai不会越界,但是如果是34+1200,Ai就会成为负数,所以此时对于34,如果Ai被检测为负数,说明到达了百位,那么它的百位和千位就都是0了.如果Ai是正数,那么就把正常的值赋值给a
	{
    
    
		a=A[Ai];
		Ai--;
	}
	int ret=a+K%10+nextnum;
	K/=10;//比如K=1234,%10,取出个位4,%10相当于取前三位进入下次循环,再取此时的个位3,以此类推
	if(ret>9)
	{
    
    
		ret-=10;//比如个位是9+9=18,那么个位的数字就是18-10=8;
		nexnum=1;//置为1,下一位就会进1
	}
	else
	{
    
    
		nexnum=0;
	}
	retarr[reti]=ret;
	++reti;//一次循环后,计算得到数字依次放到数组中
}
if(nexnum==1)
{
    
    
	retarr[reti]=1;
	++reti;//比如800+200=1000,相加时,计算到8+2的时候,已经算了三次,所以不会再进入循环,但是这一位没有进上去,所以对于这种情况要单独处理
}

Finally, since the elements are placed from 0 when they are added, that is, they are placed in order, so the final result is opposite to the actual result, so it needs to be reversed.

int left=0,right=reti-1;
while(left<right)
{
    
    
	 int temp=retarr[left];
	 retarr[left]=retarr[right];
	 retarr[right]=temp;
}

Also, the return value is an array. Be sure to pay attention to the int* returnSize of the formal parameter. It means to dereference the length of the array, otherwise the array cannot be output because there is no length.

	*returnSize=reti;
	return retarr;

Guess you like

Origin blog.csdn.net/qq_39183034/article/details/112582235