当前位置:网站首页>[delete specified number leetcode]
[delete specified number leetcode]
2022-07-28 15:36:00 【Work hard and make progress】
partners , Hello, everyone , Today I share with you for the first time in leetcode Brush the first question , If there is anything inappropriate, you are welcome to correct this rookie's mistakes .

Before the formal question brushing , We also need to know something OJ Simple classification of questions :
- IO type
- Interface type
In short IO Type is to pass scanf Enter some data , adopt printf Output some data . The interface type does not need its own input and output , You only need to implement specific functions ( You don't need to cite things like header files ), and leetcode Most of the above topics are interface type , It's very comfortable to brush , Today I experienced a , It's really comfortable .
The digression is over , Next, let's start our topic :
1 Delete the specified number
Give you an array nums And a value val, You need In situ Remove all values equal to val The elements of , And return the new length of the array after removal . Don't use extra array space , You have to use only O(1) Additional space and In situ Modify input array . The order of elements can be changed . You don't need to think about the elements in the array that follow the new length .
explain :
Why is the return value an integer , But the output answer is array ?
Please note that , The input array is based on 「 quote 」 By way of transmission , This means that modifying the input array in a function is visible to the caller .
You can imagine the internal operation as follows :
// nums In order to “ quote ” By way of transmission . in other words , Do not make any copies of the arguments
int len = removeElement(nums, val);// Modifying the input array in a function is visible to the caller .
// Based on the length returned by your function , It prints out the array Within this length All elements of .
for (int i = 0; i < len; i++) {
print(nums[i]);
}
Example 1:
Input :nums = [3,2,2,3], val = 3
Output :2, nums = [2,2]
explain : Function should return the new length 2, also nums The first two elements in are 2. You don't need to think about the elements in the array that follow the new length . for example , The new length returned by the function is 2 , and nums = [2,2,3,3] or nums = [2,2,0,0], It will also be seen as the right answer .
Example 2:
Input :nums = [0,1,2,2,3,0,4,2], val = 2
Output :5, nums = [0,1,4,0,3]
explain : Function should return the new length 5, also nums The first five elements in are 0, 1, 3, 0, 4. Note that these five elements can be in any order . You don't need to think about the elements in the array that follow the new length .
Tips :
0 <= nums.length <= 100
0 <= nums[i] <= 50
0 <= val <= 100
2 Solution 1
Traverse every element in the array , Judge whether the element is a deleted number , If it is , Just move all the elements behind the number forward by one digit , Until all arrays are traversed .
int removeElement(int* nums, int numsSize, int val)
{
int i=0;
for(i=0;i<numsSize;i++)
{
if(nums[i]==val)
{
int k=0;
for(k=i;k<numsSize-1;k++)
{
nums[k]=nums[k+1];
}
numsSize--;
i--;
}
}
return numsSize;
}This method is the easiest to think of , It's easy to implement , If implemented in this way , The time complexity is O(N^2), The space complexity is O(1), Not a particularly good method .

leetcode The execution time and memory consumption on are related to the network speed , You don't have to pay attention to this .
3 Solution 2
If we recreate an array , Traversing the target array will not delete the values of the elements and assign them to our newly created array one by one .
But this method certainly does not meet the requirements of this topic , Here is just an idea .

Specific code :
int removeElement(int* nums, int numsSize, int val)
{
int *pn=(int*)malloc(sizeof(int)*numsSize);
int i=0;
int k=0;
for(i=0;i<numsSize;i++)
{
if(nums[i]!=val)
{
pn[k++]=nums[i];
}
}
return k;
}If implemented in this way , The time complexity is O(N), But the spatial complexity is also O(N), This feels like trading space for time , This method is obviously not a good one , Is there a better way ?
4 Problem solving idea 3
Through the second way of thinking, we know that we have opened up additional space to assign the elements that are not deleted one by one , So can you assign elements to the past without opening up additional space ? The answer, of course, is yes . You can define two variables src and dest, Then iterate through the array , Compare the array with the deleted element , If not , First subscript src The value of the element of is assigned to the subscript dest Elements , let src and dest , respectively, ++, If you delete an element , Let's just src++, Give Way src Find the value in the array that is not equal to the deleted element , Put it in dest Point to the position .
Specific code implementation :
int removeElement(int* nums, int numsSize, int val)
{
int src=0;
int dest=0;
while(src<numsSize)
{
if(nums[src]!=val)
{
nums[dest]=nums[src];
src++;
dest++;
}
else
{
src++;
}
}
return dest;
}If implemented in this way , The time complexity is O(N), The space complexity is O(1), This method is obviously better than the previous two methods .

Okay , Today's sharing is here

边栏推荐
- Configure CX Oracle solution (cx_oracle.databaseerror) dpi-1047: cannot locate a 64 bit Oracle client library: "th
- 1. Author of the open source GPS project hd-gr GNSS
- Endnote 与word关联
- Daily question (retrospective)
- NFTScan 与 NFTPlay 在 NFT 数据领域达成战略合作
- 封装统一返回对象MessageResult
- 4、主程序和累积中断处理例程实现代码
- leetcode-括号有效性问题
- Solve the problem of pycharm using PowerShell
- 程序员的自我修养
猜你喜欢

vs动态库调试

Matlab导出高清图片、且Word中压缩不失真、转换PDF不失真

I heard that many merchants of crmeb have added the function of planting grass?

Here comes the full open source free customer service system

基于RSocket协议实现客户端与服务端通信

Dj-131/60c voltage relay

Multi merchant mall system with relatively clean code

day 7/12

EasyExcel复杂表头导出(一对多)

有奖活动分享:使用WordPress搭建一个专属自己的博客后最高可领取iPhone13
随机推荐
I heard that many merchants of crmeb have added the function of planting grass?
Self cultivation of programmers
全国985院校考研信息汇总整理
leetcode-括号有效性问题
.net core version 2.2 cross domain configuration
day 7/12
20、通道分配任务实现
爬虫入门(1)——requests(1)
【通道注意力机制】SENet
2、开源GPS项目HD-GR GNSS的自叙
最小堆提升每次排序的效率
DAY:7/11
3. Basic constants and macro definitions
1. Author of the open source GPS project hd-gr GNSS
Jds-12 time relay
Five connection modes of QT signal and slot
Crmeb Standard Edition window+phpstudy8 installation tutorial (II)
try...except异常处理语句(6)
[leetcode] binary search given an N-element ordered (ascending) integer array num and a target value target, write a function to search the target in num. if the target value exists, return the subscr
10、相关数据累积任务实现