当前位置:网站首页>Daily question 1: remove elements
Daily question 1: remove elements
2022-06-29 00:10:00 【Sharp blade CC】

link : Remove elements
The idea of this problem is relatively clear , We can use the double pointer method , But for the integer array, we can use the subscript to represent the pointer .
cur It is used to judge whether it is val, and tmp The function of is to count and compare with cur Data exchange at :
int cur = 0;
int tmp = 0;
Ideas :
The first thing you must do is loop through these two pointers , The cyclic condition is cur Less than numsSize( Number of original arrays ), So you can traverse the entire array .
And within the cycle , about cur, If the value at this subscript is val, Then let cur Go back to , Until you meet someone who is not val Of . And wait for cur It's not val It's about , let cur The data at is consistent with tmp Data exchange at , then tmp and cur All the way back , Continue to cycle , until cur After traversing the array .
Readers can construct according to this figure .
Code :
int removeElement(int* nums, int numsSize, int val){
int cur = 0;
int tmp = 0;
while(cur < numsSize)
{
// If it is equal cur Gaga
if(nums[cur] == val)
{
cur++;
}
// In case of inequality, exchange the two , Then add them separately
else
{
nums[tmp] = nums[cur];
tmp++;
cur++;
}
}
return tmp;
}

边栏推荐
- 毕业三年的25岁码农总结
- Phoenix installation tutorial
- Sword finger offer 12 Path in matrix
- TypeScript -- 第六节 泛型
- The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (2)
- 单机多实例MYSQL主从复制
- HandlerThread使用及原理
- 10. Yolo series
- LG. Hankson's interesting questions, C language
- 11. target segmentation
猜你喜欢

Stm32f407------- external interrupt

MySQL connection query is easy to understand

一条update语句到底加了多少锁?带你深入理解底层原理

Analysis of CSRF Cross Site Request Forgery vulnerability

12. Détection d'objets Mask rcnn

The secondary market is full of bad news. How should the market go next? One article will show you the general trend

The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (III)

Three PWN questions

【软件分析】软件分析、设计与建模迭代式详解

Notes: three ways to define setters and Getters
随机推荐
Online yaml to JSON tool
mysql 高可用双主同步
ES6 module
LinkedIn datahub - experience sharing
How many locks are added to an update statement? Take you to understand the underlying principles
Typescript -- Section 7 enumeration
Phoenix installation tutorial
Stm32f407 ------- GPIO input experiment
Give you a project, how will you carry out performance testing (I)
MSYQL is abnormal. Don't worry. Mr. Allen will point out the puzzle
12.物體檢測Mask-Rcnn
stm32F407-------串行(串口)通信
Stm32f407------- external interrupt
由背景图缓存导致的canvas绘图跨域问题
stm32F407-------外部中断
请问指南针股票软件可靠吗?在上面交易股票安全吗?
Leetcode 178 Score ranking (June 27, 2022)
After eight years of testing and opening experience and interview with 28K company, hematemesis sorted out high-frequency interview questions and answers
Is it reliable and safe to avoid five in case of stock trading account opening
每日一题: 数组中数字出现的次数