当前位置:网站首页>三道leetcode上的oj题
三道leetcode上的oj题
2022-07-31 04:36:00 【清风玉骨(「・ω・)「嘿】
目录
移除元素
给你一个数组 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;
}
}
边栏推荐
- C# 实现PLC的定时器
- Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
- PWN ROP
- MySQL数据库必会的增删查改操作(CRUD)
- Musk talks to the "virtual version" of Musk, how far is the brain-computer interaction technology from us
- Reinforcement learning: from entry to pit to shit
- $parent/$children and ref
- [CV project debugging] CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT problem
- BUG消灭者!!实用调试技巧超全整理
- unity2d game
猜你喜欢
Unity2D 自定义Scriptable Tiles的理解与使用(四)——开始着手构建一个基于Tile类的自定义tile(下)
npm、nrm两种方式查看源和切换镜像
The BP neural network
No qualifying bean of type question
Summary of Huawei Distributed Storage FusionStorage Knowledge Points [Interview]
[Paper reading] Mastering the game of Go with deep neural networks and tree search
BP神经网络
(五)final、抽象类、接口、内部类
Basic knowledge of mysql (2)
pom文件成橘红色未加载的解决方案
随机推荐
Basic knowledge of mysql (2)
Minesweeper game - C language
Go语学习笔记 - 处理超时问题 - Context使用 | 从零开始Go语言
VScode+ESP32快速安装ESP-IDF插件
MySQL数据库备份
开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
el-image标签绑定点击事件后没有有用
Gaussian distribution and its maximum likelihood estimation
Postgresql 15 source code analysis (5) - pg_control
(六)枚举、注解
C语言从入门到如土——数据的存储
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
IDEA common shortcut keys and plug-ins
已解决:不小心卸载pip后(手动安装pip的两种方式)
(6) Enumeration and annotation
Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
MySQL数据库安装配置保姆级教程(以8.0.29为例)有手就行
高等数学---第九章二重积分
Recursive implementation of the Tower of Hanoi problem
MySQL fuzzy query can use INSTR instead of LIKE