当前位置:网站首页>Three oj questions on leetcode
Three oj questions on leetcode
2022-07-31 04:52:00 【Qingfengyugu(「・ω・)「Hey】
目录
移除元素
给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度.
不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组.
元素的顺序可以改变.你不需要考虑数组中超出新长度后面的元素.
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/remove-element
解析

代码
int removeElement(int* nums, int numsSize, int val){
int src = 0;
int dst = 0;
while(src != numsSize)
{
if(nums[src] != val)
{
nums[dst] = nums[src];
++src;
++dst;
}
else
{
++src;
}
}
return dst;
}
删除有序数组中的重复项
给你一个 升序排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素只出现一次 ,返回删除后数组的新长度.元素的 相对顺序 应该保持 一致 .
由于在某些语言中不能改变数组的长度,所以必须将结果放在数组nums的第一部分.更规范地说,如果在删除重复项之后有 k 个元素,那么 nums 的前 k 个元素应该保存最终结果.
将最终结果插入 nums 的前 k 个位置后返回 k .
不要使用额外的空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成.
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/remove-duplicates-from-sorted-array
解析

代码
int removeDuplicates(int* nums, int numsSize){
int src = 0;
int dst = 0;
while( src != numsSize)
{
if(nums[src] == nums[dst])
{
++src;
}
else
{
nums[++dst] = nums[src];
++src;
}
}
return dst+1;
}
合并两个有序数组
给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目.
请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列.
注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中.为了应对这种情况,nums1 的初始长度为 m + n,其中前 m 个元素表示应合并的元素,后 n 个元素为 0 ,应忽略.nums2 的长度为 n .
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/merge-sorted-array
解析

代码
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
int end1 = m-1;
int end2 = n-1;
int i = m+n-1;
while(end1 >= 0 && end2 >= 0)
{
if(nums1[end1] < nums2[end2])
{
nums1[i] = nums2[end2];
--i;
--end2;
}
else
{
nums1[i] = nums1[end1];
--i;
--end1;
}
}
while(end2 >=0)
{
nums1[i] = nums2[end2];
--i;
--end2;
}
}
边栏推荐
- mysql基础知识(二)
- 开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
- open failed: EACCES (Permission denied)
- ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
- Open Source Smart Future | 2022 OpenAtom Global Open Source Summit OpenAtom openEuler sub-forum was successfully held
- npm、nrm两种方式查看源和切换镜像
- DVWA之SQL注入
- 重磅 | 开放原子校源行活动正式启动
- 扫雷游戏(c语言写)
- MySQL to revise the root password
猜你喜欢

C language confession code?

Open Source Database Innovation in the Digital Economy Era | 2022 Open Atom Global Open Source Summit Database Sub-Forum Successfully Held

MySQL based operations

input输入框展示两位小数之precision

Basic knowledge of mysql (2)

递归实现汉诺塔问题

idea工程明明有依赖但是文件就是显示没有,Cannot resolve symbol ‘XXX‘

三道leetcode上的oj题

ES 源码 API调用链路源码分析

Lua,ILRuntime, HybridCLR(wolong)/huatuo热更新对比分析
随机推荐
MySQL数据库备份
Basic knowledge of mysql (2)
Unity shader forge和自带的shader graph,有哪些优缺点?
产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
Sun Wenlong, Secretary General of the Open Atom Open Source Foundation |
sql语句之多表查询
The MySQL database installed configuration nanny level tutorial for 8.0.29 (for example) have hands
ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
pom文件成橘红色未加载的解决方案
ENSP,划分VLAN、静态路由,三层交换机综合配置
[R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters
three.js make 3D photo album
The idea project obviously has dependencies, but the file is not displayed, Cannot resolve symbol 'XXX'
MySQL fuzzy query can use INSTR instead of LIKE
Doris学习笔记之监控
Industry-university-research application to build an open source talent ecosystem | 2022 Open Atom Global Open Source Summit Education Sub-Forum was successfully held
MySQL修改root账号密码
HCIP第十天_BGP路由汇总实验
MySQL开窗函数
扫雷小游戏——C语言