-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy path27-remove-element.cpp
More file actions
146 lines (128 loc) · 4.76 KB
/
27-remove-element.cpp
File metadata and controls
146 lines (128 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Problem 27: Remove Element
https://leetcode.com/problems/remove-element/
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.
Consider the number of elements in nums which are not equal to val to be k. To be accepted, you need to return k, and the first k elements of nums should contain the elements which are not equal to val. The remaining elements of nums beyond the first k elements and their size are not important.
Example 1:
Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2. It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2:
Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 1, 4, 0, and 3 in some order. It does not matter what you leave beyond the returned k (hence they are underscores).
Constraints:
- 0 <= nums.length <= 100
- 0 <= nums[i] <= 50
- 0 <= val <= 100
*/
#include <vector>
#include <iostream>
#include <algorithm>
class Solution {
public:
// Approach 1: Two Pointers (when elements to remove are rare)
int removeElementTwoPointers(std::vector<int>& nums, int val) {
int i = 0;
int n = nums.size();
while (i < n) {
if (nums[i] == val) {
nums[i] = nums[n - 1]; // Move the last element to the current position
// Reduce array size by one
n--;
} else {
i++;
}
}
return n;
}
// Approach 2: Two Pointers (when elements to remove are frequent)
int removeElement(std::vector<int>& nums, int val) {
int i = 0; // Slow pointer, indicates the position to place the next non-val element
for (int j = 0; j < nums.size(); ++j) { // Fast pointer, iterates through the array
if (nums[j] != val) {
nums[i] = nums[j];
i++;
}
}
return i;
}
};
int main() {
Solution sol;
std::cout << "Testing Remove Element (Two Pointers - frequent):\n";
// Test 1
std::vector<int> nums1 = {3, 2, 2, 3};
int val1 = 3;
int k1 = sol.removeElement(nums1, val1);
std::cout << "Input: nums = [3,2,2,3], val = 3 -> Output: " << k1 << ", nums = [";
for (int i = 0; i < k1; ++i) {
std::cout << nums1[i];
if (i < k1 - 1) {
std::cout << ",";
}
}
std::cout << "] (Expected: 2, nums = [2,2])\n";
// Test 2
std::vector<int> nums2 = {0, 1, 2, 2, 3, 0, 4, 2};
int val2 = 2;
int k2 = sol.removeElement(nums2, val2);
std::cout << "Input: nums = [0,1,2,2,3,0,4,2], val = 2 -> Output: " << k2 << ", nums = [";
for (int i = 0; i < k2; ++i) {
std::cout << nums2[i];
if (i < k2 - 1) {
std::cout << ",";
}
}
std::cout << "] (Expected: 5, nums = [0,1,4,0,3])\n";
// Test 3
std::vector<int> nums3 = {};
int val3 = 0;
int k3 = sol.removeElement(nums3, val3);
std::cout << "Input: nums = [], val = 0 -> Output: " << k3 << ", nums = [";
for (int i = 0; i < k3; ++i) {
std::cout << nums3[i];
if (i < k3 - 1) {
std::cout << ",";
}
}
std::cout << "] (Expected: 0, nums = [])\n";
// Test 4
std::vector<int> nums4 = {1};
int val4 = 1;
int k4 = sol.removeElement(nums4, val4);
std::cout << "Input: nums = [1], val = 1 -> Output: " << k4 << ", nums = [";
for (int i = 0; i < k4; ++i) {
std::cout << nums4[i];
if (i < k4 - 1) {
std::cout << ",";
}
}
std::cout << "] (Expected: 0, nums = [])\n";
std::cout << "\nTesting Remove Element (Two Pointers - rare):\n";
// Test 5
std::vector<int> nums5 = {3, 2, 2, 3};
int val5 = 3;
int k5 = sol.removeElementTwoPointers(nums5, val5);
std::cout << "Input: nums = [3,2,2,3], val = 3 -> Output: " << k5 << ", nums = [";
for (int i = 0; i < k5; ++i) {
std::cout << nums5[i];
if (i < k5 - 1) {
std::cout << ",";
}
}
std::cout << "] (Expected: 2, nums = [2,2])\n";
// Test 6
std::vector<int> nums6 = {0, 1, 2, 2, 3, 0, 4, 2};
int val6 = 2;
int k6 = sol.removeElementTwoPointers(nums6, val6);
std::cout << "Input: nums = [0,1,2,2,3,0,4,2], val = 2 -> Output: " << k6 << ", nums = [";
for (int i = 0; i < k6; ++i) {
std::cout << nums6[i];
if (i < k6 - 1) {
std::cout << ",";
}
}
std::cout << "] (Expected: 5, nums = [0,1,4,0,3] or similar)\n";
return 0;
}