fork download
  1. #include <map>
  2. #include <string>
  3. #include <iostream>
  4. #include <vector>
  5. #include <stack>
  6. using namespace std;
  7.  
  8. class Solution {
  9. public:
  10. void swap(int &num1, int &num2)
  11. {
  12. int tmp;
  13. tmp =num1;
  14. num1 = num2;
  15. num2 = tmp;
  16. }
  17. int removeElement(vector<int>& nums, int val)
  18. {
  19. int left,right, cnt;
  20.  
  21. cnt = left = 0;
  22. right = nums.size();
  23. cout<<"the size of right is "<< right<<endl;
  24.  
  25. while(left < right)
  26. {
  27. if(nums[left] != val)
  28. left++;
  29. else
  30. {
  31. while(left < right && nums[right-1] == val)
  32. right--;
  33. swap(nums[left],nums[right-1]);
  34. }
  35. }
  36. return left;
  37. }
  38. };
  39.  
  40. int main()
  41. {
  42. Solution sel;
  43. vector<string> strs = {"dog","racecar","car"};
  44. vector <int> nums = {3,2,3,2};
  45. cout << "In removeElement\n";
  46. // if(sel.isPalindrome(-121))
  47. // cout<< "true";
  48. //else
  49. // cout<<"false";
  50. //cout << sel.longestCommonPrefix(strs);
  51. //sel.isValid("({{{{}}}))")? cout<< "true": cout << false;
  52. int len = sel.removeElement(nums,3);
  53. for (int i = 0; i <len ; i++)
  54. cout <<nums[i]<< endl;
  55. return 0;
  56. }
Success #stdin #stdout 0.01s 5312KB
stdin
45
stdout
In removeElement
the size of right is 4
2
3