当前位置:网站首页>每日一题:移除元素
每日一题:移除元素
2022-06-28 23:57:00 【利刃Cc】

链接: 移除元素
这道题的思路相对比较清晰,我们可以运用双指针的方法,只不过对于整形数组我们可以用下标来代表指针。
cur用于判断该处是否为val,而tmp的作用是计数以及与cur处的数据交换:
int cur = 0;
int tmp = 0;
思路:
首先肯定是要对这两个指针进行循环,循环条件就是cur小于numsSize(原数组个数数量),这样子就可以遍历整个数组。
而循环内,对于cur,如果它这个下标处的值为val,则让cur向后走,直到遇见不是val的。而等到cur走到不是val处,就让cur处的数据与tmp处的数据交换,然后tmp和cur都向后走,继续循环,直到cur遍历完数组。
读者可按该图进行思路构建.
代码:
int removeElement(int* nums, int numsSize, int val){
int cur = 0;
int tmp = 0;
while(cur < numsSize)
{
//若相等则cur加加
if(nums[cur] == val)
{
cur++;
}
//遇到不相等了就让两者交换,然后分别加加
else
{
nums[tmp] = nums[cur];
tmp++;
cur++;
}
}
return tmp;
}

边栏推荐
- Online yaml to JSON tool
- stm32F407-------电容触摸按键
- 炒股开户万一免五是靠谱么,安全么
- ctfshow XSS
- Learning fuzzy from SQL injection to bypass the latest safe dog WAF
- Typescript -- Section 7 enumeration
- How to solve the database type error in the operation of the servert project?
- Online yaml to JSON tool
- 入行数字IC验证后会做些什么?
- Phoenix installation tutorial
猜你喜欢

Easy to use free ppt template

Mobile heterogeneous computing technology - GPU OpenCL programming (basic)

10、YOLO系列

随笔记:插入排序 --from wcc

Behaviortree in ros2

What are some tips to improve your interview success rate?

三個pwn題

Yyds dry goods inventory building knowledge map from scratch with neo4j (I)

What are the virtual machine software? What are their respective roles?
![Edge extraction based on Halcon learning [2] circles Hdev routine](/img/e4/e3738d71c2ff5a239a12f67d06e2c9.jpg)
Edge extraction based on Halcon learning [2] circles Hdev routine
随机推荐
在线买股票开户安全嘛?
Thinking about lever
After eight years of testing and opening experience and interview with 28K company, hematemesis sorted out high-frequency interview questions and answers
转载:VTK笔记-裁剪分割-三维曲线或几何切割体数据(黑山老妖)
urllib. Parse parses the parameters in the URL connection
fio的IO重放功能
The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (2)
mysql 8.0以上报2058 解决方式
stm32F407-------通用定时器
TypeScript --第三节:接口
Don't ask me how to do UI automation test again
Association line exploration, how to connect the two nodes of the flow chart
stm32F407-------LCD
websocket-js连接如何携带token验证
[software analysis] iterative explanation of software analysis, design and modeling
Common mistakes in software testing
stm32F407-------NVIC中断优先级管理
WPF implementation calls local camera~
Sword finger offer 12 Path in matrix
Give you a project, how will you carry out performance testing (I)