Skip to content

Commit bb03300

Browse files
authored
Create Apply Operations to an Array.cpp
1 parent 54c3b96 commit bb03300

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

easy/Apply Operations to an Array.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//Apply Operations to an array
2+
//This question has been asked in leetcode contest and I have given its solution in C++
3+
class Solution {
4+
public:
5+
vector<int> applyOperations(vector<int>& nums) {
6+
for(int i=0; i<nums.size()-1;i++) {
7+
if(nums[i]==nums[i+1]) {
8+
nums[i] = nums[i]*2;
9+
nums[i+1] = 0;
10+
}
11+
}
12+
vector<int> num;
13+
int count = 0;
14+
for(int i=0; i<nums.size();i++) {
15+
if(nums[i] != 0) {
16+
num.push_back(nums[i]);
17+
count++;
18+
}
19+
}
20+
for(int i=0; i<nums.size()-count; i++) {
21+
num.push_back(0);
22+
}
23+
return num;
24+
}
25+
};

0 commit comments

Comments
 (0)