当前位置:网站首页>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;
}
}
边栏推荐
- ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)
- Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays
- Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
- 矩池云快速安装torch-sparse、torch-geometric等包
- sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
- C Implementation of Simple Network File Copy
- Lua,ILRuntime, HybridCLR(wolong)/huatuo热更新对比分析
- 【wpf】wpf中的那些模板之深度解析
- Puzzle Game Level Design: Reverse Method--Explaining Puzzle Game Level Design
- three.js make 3D photo album
猜你喜欢
XSS靶场(三)prompt to win
扫雷小游戏——C语言
Hand in hand to realize the picture preview plug-in (3)
专访 | 阿里巴巴首席技术官程立:云+开源共同形成数字世界的可信基础
PWN ROP
Musk talks to the "virtual version" of Musk, how far is the brain-computer interaction technology from us
【C语言进阶】文件操作(一)
On Governance and Innovation | 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum was successfully held
马斯克对话“虚拟版”马斯克,脑机交互技术离我们有多远
WPF WPF 】 【 the depth resolution of the template
随机推荐
DVWA安装教程(懂你的不懂·详细)
three.js make 3D photo album
Puzzle Game Level Design: Reverse Method--Explaining Puzzle Game Level Design
Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
View source and switch mirrors in two ways: npm and nrm
Unity打灵狐者
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution
Win10 CUDA CUDNN installation configuration (torch paddlepaddle)
益智类游戏关卡设计:逆推法--巧解益智类游戏关卡设计
MySQL修改root账号密码
Industry landing presents new progress | 2022 OpenAtom Global Open Source Summit OpenAtom OpenHarmony sub-forum was successfully held
MySQL常见面试题汇总(建议收藏!!!)
[Cloud Native] DevOps (5): Integrating Harbor
ENSP,划分VLAN、静态路由,三层交换机综合配置
MySQL数据库安装配置保姆级教程(以8.0.29为例)有手就行
sql语句之多表查询
Go language study notes - dealing with timeout problems - Context usage | Go language from scratch
开源社区三十年 | 2022开放原子全球开源峰会开源社区三十年专题活动圆满召开
MySQL开窗函数