gha890826

LeetCode-26

Dec 13th, 2019 (edited)
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. // ConsoleApplication7.cpp : 定義主控台應用程式的進入點。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. int removeDuplicates(vector<int>& nums)
  10. {
  11.     if (nums.size() == 0)
  12.         return 0;
  13.     int ans = 0;
  14.     for (int i = 0; i < nums.size()-1;)
  15.     {
  16.         cout << "***i=" << i << endl;
  17.         if (nums[i] == nums[i + 1])
  18.         {
  19.             ans++;
  20.             nums.erase(nums.begin() + i + 1);
  21.         }
  22.         else
  23.             i++;
  24.     }
  25.     for (int i = 0; i < nums.size(); i++) {
  26.         cout<<nums[i];
  27.     }
  28.     cout << endl;
  29.     return ans;
  30. }
  31.  
  32. int main()
  33. {
  34.     vector<int> in1 = { 0, 0, 1, 1, 1, 2, 2, 3, 3, 4 },in2(0);
  35.     cout << removeDuplicates(in1) << endl;
  36.     cout << removeDuplicates(in2) << endl;
  37.     return 0;
  38. }
Add Comment
Please, Sign In to add comment