/
DeC2018
/
0027
Обзор
Документация
Войти
/
DeC2018
/
0027
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
main.cpp
51 строка
1 KB
Den
create main.cpp
31 дек 2024, 12:53
31 дек 2024, 12:53
47e8317
Код
Авторство
О чём код?
#include <iostream> #include <vector> #include <algorithm> using namespace std; class Solution { public: int removeElement(vector<int>& nums, int val) { int i = 0; for (const int num : nums) { if (num != val) { nums[i++] = num; // Place the non-val elements at the front } } return i; // Return the count of non-val elements } }; int main() { Solution solution; // Example 1 vector<int> nums1 = {3, 2, 2, 3}; int val1 = 3; int k1 = solution.removeElement(nums1, val1); cout << "Output: " << k1 << ", nums = ["; for (int i = 0; i < k1; i++) { cout << nums1[i] << (i < k1 - 1 ? ", " : ""); } for (int i = k1; i < nums1.size(); i++) { cout << ", _"; } cout << "]" << endl; // Example 2 vector<int> nums2 = {0, 1, 2, 2, 3, 0, 4, 2}; int val2 = 2; int k2 = solution.removeElement(nums2, val2); cout << "Output: " << k2 << ", nums = ["; for (int i = 0; i < k2; i++) { cout << nums2[i] << (i < k2 - 1 ? ", " : ""); } for (int i = k2; i < nums2.size(); i++) { cout << ", _"; } cout << "]" << endl; return 0; }