当前位置:网站首页>三道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;
}
}
边栏推荐
- WeChat applet uses cloud functions to update and add cloud database nested array elements
- 【C语言进阶】文件操作(一)
- Industry landing presents new progress | 2022 OpenAtom Global Open Source Summit OpenAtom OpenHarmony sub-forum was successfully held
- Safety 20220709
- three.js 制作3D相册
- Open Source Smart Future | 2022 OpenAtom Global Open Source Summit OpenAtom openEuler sub-forum was successfully held
- Learning DAVID Database (1)
- 简易网络文件拷贝的C实现
- type_traits metaprogramming library learning
- [AUTOSAR-RTE]-5-Explicit (explicit) and Implicit (implicit) Sender-Receiver communication
猜你喜欢
HCIP Day 10_BGP Route Summary Experiment
IDEA common shortcut keys and plug-ins
"A daily practice, happy water problem" 1331. Array serial number conversion
VScode+ESP32快速安装ESP-IDF插件
Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code
【wpf】wpf中的那些模板之深度解析
LocalDate addition and subtraction operations and comparison size
【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
扫雷小游戏——C语言
已解决:不小心卸载pip后(手动安装pip的两种方式)
随机推荐
Why don't you programmers make a living off your own projects?And have to work for someone else?
The use of beforeDestroy and destroyed
Recursive implementation of the Tower of Hanoi problem
C language from entry to such as soil, the data store
MySQL based operations
VScode+ESP32 quickly install ESP-IDF plugin
open failed: EACCES (Permission denied)
MySQL修改root账号密码
[Paper reading] Mastering the game of Go with deep neural networks and tree search
Unity Fighter
数字经济时代的开源数据库创新 | 2022开放原子全球开源峰会数据库分论坛圆满召开
mysql数据库安装(详细)
简易网络文件拷贝的C实现
Safety 20220712
关于出现大量close_wait状态的理解
C language confession code?
【线性神经网络】softmax回归
Smartcom Programming Level 4 - Magic Academy Lesson 6
pom文件成橘红色未加载的解决方案
three.js make 3D photo album