经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
Leetcode No.26 Remove Duplicates from Sorted Array(c++实现)
来源:cnblogs  作者:云梦士  时间:2021/6/28 9:42:30  对本文有异议

1. 题目

1.1 英文题目

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-placein-place such that each unique element appears only once. The relative order of the elements should be kept the same.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

1.2 中文题目

给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。

1.3输入输出

输入 输出
nums = [1,1,2] 2, nums = [1,2,_]
[0,0,1,1,1,2,2,3,3,4] 5, nums = [0,1,2,3,4,,,,,_]

2. 实验平台

IDE:VS2019
IDE版本:16.10.1
语言:c++11

3. 程序

3.1 测试程序

  1. #include "Solution.h"
  2. #include <vector> // std::vector
  3. #include<iostream> // std::cout
  4. using namespace std;
  5. // 主程序
  6. void main()
  7. {
  8. vector<int> nums = { 0,0,0,1,1,1,2,2,3,4,4 }; // 输入
  9. Solution solution; // 实例化Solution
  10. int k = solution.removeDuplicates(nums); // 主功能
  11. // 输出
  12. cout << k << ", [";
  13. for (int i = 0; i < k; i++)
  14. {
  15. if (i == k - 1)
  16. cout << nums[i] << "]";
  17. else
  18. cout << nums[i] << ",";
  19. }
  20. }

3.2 功能程序

3.2.1 最佳程序

(1)代码

  1. #pragma once
  2. #include<vector> // std::vector
  3. using namespace std;
  4. //主功能
  5. class Solution {
  6. public:
  7. int removeDuplicates(vector<int>& nums)
  8. {
  9. if (nums.size() > 1)
  10. {
  11. int i; // i为慢指针,指向的是去重后最后一位,随j更新
  12. int j; // j为快指针,不断向前探索
  13. for (i = 0, j = 0; j < nums.size(); j++)
  14. {
  15. if (nums[i] != nums[j]) // 若快指针所指元素与慢指针相同,则慢的不动,快的前进一步
  16. nums[++i] = nums[j]; // 若不同,则慢的前进一步指向快指针指的元素,快指针继续向前一步
  17. }
  18. return i + 1; // 因为下标从0开始,所以去重后的元素个数为i+1
  19. }
  20. }
  21. };

此程序参考:https://blog.csdn.net/qjh5606/article/details/81434396

(2)解读

快慢指针,慢的指向的一直都是新的,也就是说来一个新的它才往前走一步,快的就是不断向前,发现和慢的不一样的就汇报给那个慢的,然后慢的更新。如果二者相等,则慢的不动,快的前进一步;如果二者不等,则慢的前进一步的同时更新那个新一步指向的数字为现在快指针指向的新数字,快的同时也往前走一步。

解读参考:https://www.cnblogs.com/forPrometheus-jun/p/10889152.html

3.2.2 自写程序

(1)代码

  1. #pragma once
  2. #include<vector> // std::vector
  3. #include<algorithm> // std::find
  4. using namespace std;
  5. //主功能
  6. class Solution {
  7. public:
  8. int removeDuplicates(vector<int>& nums)
  9. {
  10. if (nums.size() > 1) // 若nums不是空集合或单元素集合
  11. {
  12. int i = 1;
  13. while (i != nums.size())
  14. {
  15. if (nums[i] == nums[i - 1]) // 若元素重复
  16. nums.erase(find(nums.begin(), nums.end(), nums[i])); //则去除重复数字
  17. else // 若元素不重复
  18. i += 1; // 则向后再查看一个元素
  19. }
  20. }
  21. return nums.size(); // 返回结果
  22. }
  23. };

(2)思路

从前往后遍历,如果发现有元素重复,则将重复元素剔除

3.2.3 其他程序

(1)代码

  1. #pragma once
  2. #include<vector> // std::vector
  3. #include<algorithm> // std::unique
  4. using namespace std;
  5. class Solution {
  6. public:
  7. int removeDuplicates(vector<int>& nums) {
  8. nums.erase(std::unique(nums.begin(), nums.end()), nums.end());
  9. return nums.size();
  10. }
  11. };

(2)解读

该程序直接使用 c++标准模板库STL中的函数std::unique查找重复元素,更简洁。

4. 相关知识

(1) 快慢指针(Fast-Slow Pointer)

这几个教程挺不错的,可以参考一下:
https://www.cnblogs.com/hxsyl/p/4395794.html
https://blog.csdn.net/SoftPoeter/article/details/103153564
https://zhuanlan.zhihu.com/p/361049436

(2) i++与++i的区别

  • i++ 即后加加,原理是:先自增,然后返回自增之前的值
  • ++i 即前加加,原理是:先自增,然后返回自增之后的值

参考:https://blog.csdn.net/android_cai_niao/article/details/106027313

原文链接:http://www.cnblogs.com/yunmeng-shi/p/14927735.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号