We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c25f01b commit 88ecb65Copy full SHA for 88ecb65
Easy/Apply Operations to an Array.java
@@ -1,21 +1,20 @@
1
class Solution {
2
public int[] applyOperations(int[] nums) {
3
- for (int i = 0; i < nums.length - 1; i++) {
+ int n = nums.length;
4
+ for (int i = 0; i < n - 1; i++) {
5
if (nums[i] == nums[i + 1]) {
6
nums[i] *= 2;
7
nums[i + 1] = 0;
8
}
9
- int startIdx = 0;
10
- int endIdx = 0;
11
- while (endIdx < nums.length) {
12
- if (nums[endIdx] != 0) {
13
- nums[startIdx++] = nums[endIdx];
+ int start = 0;
+ for (int i = 0; i < n; i++) {
+ if (nums[i] != 0) {
+ nums[start++] = nums[i];
14
15
- endIdx++;
16
17
- while (startIdx < nums.length) {
18
- nums[startIdx++] = 0;
+ while (start < n) {
+ nums[start++] = 0;
19
20
return nums;
21
0 commit comments