当前位置:网站首页>三道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;
}
}
边栏推荐
- WPF WPF 】 【 the depth resolution of the template
- Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays
- C language from entry to such as soil, the data store
- 简易网络文件拷贝的C实现
- Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
- 聚变云原生,赋能新里程 | 2022开放原子全球开源峰会云原生分论坛圆满召开
- MySQL模糊查询可以使用INSTR替代LIKE
- Why don't you programmers make a living off your own projects?And have to work for someone else?
- HCIP Day 10_BGP Route Summary Experiment
- Fusion Cloud Native, Empowering New Milestones | 2022 Open Atom Global Open Source Summit Cloud Native Sub-Forum Successfully Held
猜你喜欢
LocalDate addition and subtraction operations and comparison size
Why don't you programmers make a living off your own projects?And have to work for someone else?
Hand in hand to realize the picture preview plug-in (3)
type_traits metaprogramming library learning
Fusion Cloud Native, Empowering New Milestones | 2022 Open Atom Global Open Source Summit Cloud Native Sub-Forum Successfully Held
Thinking about data governance after Didi fines
Summary of Huawei Distributed Storage FusionStorage Knowledge Points [Interview]
ENSP,划分VLAN、静态路由,三层交换机综合配置
type_traits元编程库学习
Go语学习笔记 - 处理超时问题 - Context使用 | 从零开始Go语言
随机推荐
Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code
强化学习:从入门到入坑再到拉屎
微软 AI 量化投资平台 Qlib 体验
Understanding and Using Unity2D Custom Scriptable Tiles (4) - Start to build a custom tile based on the Tile class (below)
MySQL数据库备份
idea工程明明有依赖但是文件就是显示没有,Cannot resolve symbol ‘XXX‘
Regarding the primary key id in the mysql8.0 database, when the id is inserted using replace to be 0, the actual id is automatically incremented after insertion, resulting in the solution to the repea
STM32HAL库修改Hal_Delay为us级延时
产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
mysql数据库安装(详细)
[shell basics] determine whether the directory is empty
Vue项目通过node连接MySQL数据库并实现增删改查操作
open failed: EACCES (Permission denied)
ClickHouse:设置远程连接
interprocess communication
Smartcom Programming Level 4 - Magic Academy Lesson 6
Safety 20220722
MySQL基础操作
three.js make 3D photo album
Gaussian distribution and its maximum likelihood estimation