Skip to content

Commit b6fb231

Browse files
author
Danieldu
committed
add 2460
1 parent e88d463 commit b6fb231

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @lc app=leetcode id=2460 lang=cpp
3+
*
4+
* [2460] Apply Operations to an Array
5+
*/
6+
7+
// @lc code=start
8+
#include "bits/stdc++.h"
9+
using namespace std;
10+
11+
class Solution {
12+
public:
13+
vector<int> applyOperations(vector<int>& nums) {
14+
int size = nums.size();
15+
for(int i=0;i<size-1;i++){
16+
if(nums[i] && nums[i]==nums[i+1]){
17+
nums[i]*=2;
18+
nums[i+1]=0;
19+
}
20+
}
21+
int pos = 0;
22+
for(int i = 0; i < size; i++) {
23+
if (nums[i] != 0) {
24+
swap(nums[i], nums[pos]);
25+
pos++;
26+
}
27+
}
28+
return nums;
29+
}
30+
};
31+
// @lc code=end
32+

0 commit comments

Comments
 (0)