当前位置: 首页 > news >正文

leetcode26:删除有序数组中的重复项

leetcode26:删除有序数组中的重复项

方案一:依次遍历,如果不符合条件则冒泡交换到最后一个位置。o(n^2),结果超时

#include <algorithm>
#include <iostream>using namespace std;
class Solution {
public:int removeDuplicates(vector<int>& nums) {if (nums.size() == 1) {return 1;}int dupNum = 0;int i = 1;int pre = nums[0];while (i < nums.size() - dupNum) {int now = nums[i];if (now == pre) {for (int j = i; j < nums.size() - dupNum - 1; j++) {std::swap(nums[j], nums[j+1]);}dupNum++;} else {i++;}pre = now;}return nums.size() - dupNum;}
};

方案二:迭代器遍历直接删除。通过✅

在这里插入代码片#include <algorithm>
#include <iostream>using namespace std;
class Solution {
public:int removeDuplicates(vector<int>& nums) {if (nums.size() == 1) {return 1;}vector<int>::iterator ite = nums.begin();int pre = *ite;ite++;while (ite != nums.end()) {int now = *ite;if (now == pre) {ite = nums.erase(ite);}else {ite++;}pre = now;}return nums.size();}
};
http://www.lryc.cn/news/208617.html

相关文章:

  • [FSCTF 2023] web题解
  • linux查看内存的方式
  • Python 编写 Flink 应用程序经验记录(Flink1.17.1)
  • 如何 通过使用优先级提示,来控制所有网页资源加载顺序
  • 10月25日,每日信息差
  • 泛微OA之获取每月固定日期
  • Dataworks API:调取 MC 项目下所有表单
  • Node编写更新用户头像接口
  • MySQL3:MySQL中一条更新SQL是如何执行的?
  • p5.js map映射
  • idea提交代码冲突后,代码意外消失解决办法
  • 爬虫批量下载科研论文(SciHub)
  • explain查询sql执行计划返回的字段的详细说明
  • 讯飞输入法13.0发布,推出行业首款生成式AI输入法
  • 35. 搜索插入位置、Leetcode的Python实现
  • 使用 DDPO 在 TRL 中微调 Stable Diffusion 模型
  • cocosCreator 之 crypto-es数据加密
  • Leetcode---368周赛
  • 矢量图形编辑软件Illustrator 2023 mac中文版软件特点(ai2023) v27.9
  • 一、Docker Compose——什么是 Docker Compose
  • Java提升技术,进阶为高级开发和架构师的路线
  • 记一次 .Net+SqlSugar 查询超时的问题排查过程
  • PHP危险函数
  • 【ARM Cortex-M 系列 4 番外篇 -- 常用 benchmark 介绍】
  • web安全-原发抗抵赖
  • 强化学习------PPO算法
  • node(三)express框架
  • linux find命令搜索日志内容
  • CentOS 编译安装TinyXml2
  • 竞赛选题 深度学习人体跌倒检测 -yolo 机器视觉 opencv python