Hello!很高兴又见到你了~~~
看看今天要学点什么来充实大脑吧------

目录
[至此结束,Show Time!](#至此结束,Show Time!)
1、移除元素


【思路+图解】


上面的例子第一个数是等于val,当不等于val 时可以自己试一下。新思路的时间复杂度为O(N),空间复杂度为O(N),结合思路我们尝试上代码:

cs
int removeElement(int* nums, int numsSize, int val)
{
int src=0;
int dst=0;
while(src<numsSize)
{
if(nums[src]==val)
{
src++;
}
else
{
nums[dst++]=nums[src++];
//src++;
//dst++;
}
}
return dst;
}
【总结】
通过把 src 指向的不等于 val 的数赋给 dst 指向的位置,实现把不等于 val 的数放到前面,而且刚好 dst 的值就是数组中不等于 val 的数据的个数,同时要注意循环的条件。
2、删除有序数组中的重复项

【思路+图解】


咱们直接上代码:

在图解第一个步骤中出现了重复赋值的情况。经过 dst +1 后,dst 指向的数据等于2,src 指向的数据也等于2,再把 src 指向数据的值赋给dst,这里出现了重复赋值,我们优化一下代码:

cs
int removeDuplicates(int* nums, int numsSize)
{
int dst=0;
int src=1;
while(src<numsSize)
{
if(nums[dst]!=nums[src]&&++dst!=src)
{
nums[dst]=nums[src];
}
src++;
}
return dst+1;
}
【总结】
通过 src 遍历数组,如果与 dst 指向的数据相等就跳过,不相等就先 dst++,再赋给 dst 指向的位置,限制条件 src<数组长度。这种方法思路清晰!
3、合并两个有序数组

【思路+图解】


思路理清,上代码:

cs
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n)
{
int l1=m-1;
int l2=n-1;
int l3=m+n-1;
while(l1>=0&&l2>=0)
{
if(nums1[l1]>nums2[l2])
{
nums1[l3--]=nums1[l1--];
}
else
{
nums1[l3--]=nums2[l2--];
}
}
//出了while循环,要么l1<0,要么l2<0
while(l2>=0)
{
nums1[l3--]=nums2[l2--];
}
}
【总结】
妙!多理解理解,记住这种方法,悟一悟,反正我太菜想不到这种方法......
至此结束,Show Time!
完------
