Skip to content

Commit 4c74b4d

Browse files
committed
feat: add solutions to lc problem: No.2460
No.2460.Apply Operations to an Array
1 parent 114f69b commit 4c74b4d

File tree

7 files changed

+154
-96
lines changed

7 files changed

+154
-96
lines changed

solution/2400-2499/2460.Apply Operations to an Array/README.md

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,15 @@
6363

6464
**方法一:模拟**
6565

66-
直接根据题意模拟即可
66+
我们直接根据题意模拟即可
6767

68-
时间复杂度 $O(n)$,忽略答案的空间消耗,空间复杂度 $O(1)$。其中 $n$ 是数组 `nums` 的长度。
68+
首先,我们遍历数组 $nums$,对于任意相邻的两个元素 $nums[i]$ 和 $nums[i+1]$,如果 $nums[i]=nums[i+1]$,那么我们将 $nums[i]$ 的值变为原来的 $2$ 倍,同时将 $nums[i+1]$ 的值变为 $0$。
69+
70+
然后,我们创建一个长度为 $n$ 的答案数组 $ans$,并将 $nums$ 中所有非零元素按顺序放入 $ans$ 中。
71+
72+
最后,返回答案数组 $ans$ 即可。
73+
74+
时间复杂度 $O(n)$,其中 $n$ 是数组 $nums$ 的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。
6975

7076
<!-- tabs:start -->
7177

@@ -81,8 +87,12 @@ class Solution:
8187
if nums[i] == nums[i + 1]:
8288
nums[i] <<= 1
8389
nums[i + 1] = 0
84-
ans = [v for v in nums if v]
85-
ans += [0] * (n - len(ans))
90+
ans = [0] * n
91+
i = 0
92+
for x in nums:
93+
if x:
94+
ans[i] = x
95+
i += 1
8696
return ans
8797
```
8898

@@ -131,9 +141,12 @@ public:
131141
}
132142
}
133143
vector<int> ans(n);
134-
int j = 0;
135-
for (int i = 0; i < n; ++i) if (nums[i]) ans[j++] = nums[i];
136-
for (int i = 0; i < n; ++i) if (!nums[i]) ans[j++] = nums[i];
144+
int i = 0;
145+
for (int& x : nums) {
146+
if (x) {
147+
ans[i++] = x;
148+
}
149+
}
137150
return ans;
138151
}
139152
};
@@ -142,32 +155,46 @@ public:
142155
### **Go**
143156
144157
```go
145-
func applyOperations(nums []int) (ans []int) {
146-
n := len(nums)
147-
for i := 0; i < n - 1; i++ {
148-
if nums[i] == nums[i + 1] {
149-
nums[i] <<= 1
150-
nums[i + 1] = 0
151-
}
152-
}
153-
for _, v := range nums {
154-
if v != 0 {
155-
ans = append(ans, v)
156-
}
157-
}
158-
for _, v := range nums {
159-
if v == 0 {
160-
ans = append(ans, v)
161-
}
162-
}
163-
return
158+
func applyOperations(nums []int) []int {
159+
n := len(nums)
160+
for i := 0; i < n-1; i++ {
161+
if nums[i] == nums[i+1] {
162+
nums[i] <<= 1
163+
nums[i+1] = 0
164+
}
165+
}
166+
ans := make([]int, n)
167+
i := 0
168+
for _, x := range nums {
169+
if x > 0 {
170+
ans[i] = x
171+
i++
172+
}
173+
}
174+
return ans
164175
}
165176
```
166177

167178
### **TypeScript**
168179

169180
```ts
170-
181+
function applyOperations(nums: number[]): number[] {
182+
const n = nums.length;
183+
for (let i = 0; i < n - 1; ++i) {
184+
if (nums[i] === nums[i + 1]) {
185+
nums[i] <<= 1;
186+
nums[i + 1] = 0;
187+
}
188+
}
189+
const ans: number[] = Array(n).fill(0);
190+
let i = 0;
191+
for (const x of nums) {
192+
if (x !== 0) {
193+
ans[i++] = x;
194+
}
195+
}
196+
return ans;
197+
}
171198
```
172199

173200
### **...**

solution/2400-2499/2460.Apply Operations to an Array/README_EN.md

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ class Solution:
6767
if nums[i] == nums[i + 1]:
6868
nums[i] <<= 1
6969
nums[i + 1] = 0
70-
ans = [v for v in nums if v]
71-
ans += [0] * (n - len(ans))
70+
ans = [0] * n
71+
i = 0
72+
for x in nums:
73+
if x:
74+
ans[i] = x
75+
i += 1
7276
return ans
7377
```
7478

@@ -85,15 +89,10 @@ class Solution {
8589
}
8690
}
8791
int[] ans = new int[n];
88-
int j = 0;
89-
for (int i = 0; i < n; ++i) {
90-
if (nums[i] != 0) {
91-
ans[j++] = nums[i];
92-
}
93-
}
94-
for (int i = 0; i < n; ++i) {
95-
if (nums[i] == 0) {
96-
ans[j++] = nums[i];
92+
int i = 0;
93+
for (int x : nums) {
94+
if (x > 0) {
95+
ans[i++] = x;
9796
}
9897
}
9998
return ans;
@@ -115,9 +114,12 @@ public:
115114
}
116115
}
117116
vector<int> ans(n);
118-
int j = 0;
119-
for (int i = 0; i < n; ++i) if (nums[i]) ans[j++] = nums[i];
120-
for (int i = 0; i < n; ++i) if (!nums[i]) ans[j++] = nums[i];
117+
int i = 0;
118+
for (int& x : nums) {
119+
if (x) {
120+
ans[i++] = x;
121+
}
122+
}
121123
return ans;
122124
}
123125
};
@@ -126,32 +128,46 @@ public:
126128
### **Go**
127129
128130
```go
129-
func applyOperations(nums []int) (ans []int) {
130-
n := len(nums)
131-
for i := 0; i < n - 1; i++ {
132-
if nums[i] == nums[i + 1] {
133-
nums[i] <<= 1
134-
nums[i + 1] = 0
135-
}
136-
}
137-
for _, v := range nums {
138-
if v != 0 {
139-
ans = append(ans, v)
140-
}
141-
}
142-
for _, v := range nums {
143-
if v == 0 {
144-
ans = append(ans, v)
145-
}
146-
}
147-
return
131+
func applyOperations(nums []int) []int {
132+
n := len(nums)
133+
for i := 0; i < n-1; i++ {
134+
if nums[i] == nums[i+1] {
135+
nums[i] <<= 1
136+
nums[i+1] = 0
137+
}
138+
}
139+
ans := make([]int, n)
140+
i := 0
141+
for _, x := range nums {
142+
if x > 0 {
143+
ans[i] = x
144+
i++
145+
}
146+
}
147+
return ans
148148
}
149149
```
150150

151151
### **TypeScript**
152152

153153
```ts
154-
154+
function applyOperations(nums: number[]): number[] {
155+
const n = nums.length;
156+
for (let i = 0; i < n - 1; ++i) {
157+
if (nums[i] === nums[i + 1]) {
158+
nums[i] <<= 1;
159+
nums[i + 1] = 0;
160+
}
161+
}
162+
const ans: number[] = Array(n).fill(0);
163+
let i = 0;
164+
for (const x of nums) {
165+
if (x !== 0) {
166+
ans[i++] = x;
167+
}
168+
}
169+
return ans;
170+
}
155171
```
156172

157173
### **...**

solution/2400-2499/2460.Apply Operations to an Array/Solution.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ class Solution {
99
}
1010
}
1111
vector<int> ans(n);
12-
int j = 0;
13-
for (int i = 0; i < n; ++i)
14-
if (nums[i]) ans[j++] = nums[i];
15-
for (int i = 0; i < n; ++i)
16-
if (!nums[i]) ans[j++] = nums[i];
12+
int i = 0;
13+
for (int& x : nums) {
14+
if (x) {
15+
ans[i++] = x;
16+
}
17+
}
1718
return ans;
1819
}
1920
};
Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
func applyOperations(nums []int) (ans []int) {
2-
n := len(nums)
3-
for i := 0; i < n - 1; i++ {
4-
if nums[i] == nums[i + 1] {
5-
nums[i] <<= 1
6-
nums[i + 1] = 0
7-
}
8-
}
9-
for _, v := range nums {
10-
if v != 0 {
11-
ans = append(ans, v)
12-
}
13-
}
14-
for _, v := range nums {
15-
if v == 0 {
16-
ans = append(ans, v)
17-
}
18-
}
19-
return
1+
func applyOperations(nums []int) []int {
2+
n := len(nums)
3+
for i := 0; i < n-1; i++ {
4+
if nums[i] == nums[i+1] {
5+
nums[i] <<= 1
6+
nums[i+1] = 0
7+
}
8+
}
9+
ans := make([]int, n)
10+
i := 0
11+
for _, x := range nums {
12+
if x > 0 {
13+
ans[i] = x
14+
i++
15+
}
16+
}
17+
return ans
2018
}

solution/2400-2499/2460.Apply Operations to an Array/Solution.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@ public int[] applyOperations(int[] nums) {
88
}
99
}
1010
int[] ans = new int[n];
11-
int j = 0;
12-
for (int i = 0; i < n; ++i) {
13-
if (nums[i] != 0) {
14-
ans[j++] = nums[i];
15-
}
16-
}
17-
for (int i = 0; i < n; ++i) {
18-
if (nums[i] == 0) {
19-
ans[j++] = nums[i];
11+
int i = 0;
12+
for (int x : nums) {
13+
if (x > 0) {
14+
ans[i++] = x;
2015
}
2116
}
2217
return ans;

solution/2400-2499/2460.Apply Operations to an Array/Solution.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ def applyOperations(self, nums: List[int]) -> List[int]:
55
if nums[i] == nums[i + 1]:
66
nums[i] <<= 1
77
nums[i + 1] = 0
8-
ans = [v for v in nums if v]
9-
ans += [0] * (n - len(ans))
8+
ans = [0] * n
9+
i = 0
10+
for x in nums:
11+
if x:
12+
ans[i] = x
13+
i += 1
1014
return ans
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function applyOperations(nums: number[]): number[] {
2+
const n = nums.length;
3+
for (let i = 0; i < n - 1; ++i) {
4+
if (nums[i] === nums[i + 1]) {
5+
nums[i] <<= 1;
6+
nums[i + 1] = 0;
7+
}
8+
}
9+
const ans: number[] = Array(n).fill(0);
10+
let i = 0;
11+
for (const x of nums) {
12+
if (x !== 0) {
13+
ans[i++] = x;
14+
}
15+
}
16+
return ans;
17+
}

0 commit comments

Comments
 (0)