码迷,mamicode.com
首页 > 编程语言 > 详细

力扣(LeetCode)试题26-删除排序数组中的重复项 C++代码

时间:2020-07-03 10:30:08      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:out   span   http   remove   for   cto   amp   eps   size   

ε=(´ο`*)))唉,继续加油,先实现功能

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 class Solution {
 7 public:
 8     int removeDuplicates(vector<int>& nums) 
 9     {
10         int val = nums.size();
11         int i = 0;
12         for (int j = 1; j < val; j++)
13         {
14             if (nums[i] == nums[j])
15             {
16                 continue;
17             }
18             else
19             {
20                 nums[i+1] = nums[j];
21                 i += 1;
22             }
23         }
24         return i+1;
25     }
26 };
27 
28 int main()
29 {
30     vector<int> nums = { 0, 0, 1, 1, 1, 2, 2, 3, 3, 4 };
31     int length;
32     Solution sol;
33     length = sol.removeDuplicates(nums);
34     cout << length << endl;;
35     int n;
36     for (int i = 0; i < length; i++)
37     {
38         cout << nums[i] << endl;
39     }
40     cin >> n;
41     return 0;
42 }

技术图片

 

技术图片

 

力扣(LeetCode)试题26-删除排序数组中的重复项 C++代码

标签:out   span   http   remove   for   cto   amp   eps   size   

原文地址:https://www.cnblogs.com/pgzhanglin/p/13228737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!