Skip to content

Commit 9e6ca66

Browse files
authored
feat: add solutions to lc problem: No.1313 (doocs#3489)
No.1313.Decompress Run-Length Encoded List
1 parent 75d47fc commit 9e6ca66

File tree

10 files changed

+124
-121
lines changed

10 files changed

+124
-121
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
.DS_Store
33
.vscode
44
.temp
5+
.vitepress
56
.cache
67
*.iml
78
__pycache__
89
/node_modules
910
/solution/result.json
1011
/solution/__pycache__
11-
/solution/.env
12+
/solution/.env

solution/1300-1399/1313.Decompress Run-Length Encoded List/README.md

+41-39
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ tags:
5858

5959
<!-- solution:start -->
6060

61-
### 方法一
61+
### 方法一:模拟
62+
63+
我们可以直接模拟题目描述的过程,从左到右遍历数组 $\textit{nums}$,每次取出两个数 $\textit{freq}$ 和 $\textit{val}$,然后将 $\textit{val}$ 重复 $\textit{freq}$ 次,将这 $\textit{freq}$ 个 $\textit{val}$ 加入答案数组即可。
64+
65+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。我们只需要遍历一次数组 $\textit{nums}$ 即可。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
6266

6367
<!-- tabs:start -->
6468

@@ -67,28 +71,21 @@ tags:
6771
```python
6872
class Solution:
6973
def decompressRLElist(self, nums: List[int]) -> List[int]:
70-
res = []
71-
for i in range(1, len(nums), 2):
72-
res.extend([nums[i]] * nums[i - 1])
73-
return res
74+
return [nums[i + 1] for i in range(0, len(nums), 2) for _ in range(nums[i])]
7475
```
7576

7677
#### Java
7778

7879
```java
7980
class Solution {
8081
public int[] decompressRLElist(int[] nums) {
81-
int n = 0;
82+
List<Integer> ans = new ArrayList<>();
8283
for (int i = 0; i < nums.length; i += 2) {
83-
n += nums[i];
84-
}
85-
int[] res = new int[n];
86-
for (int i = 1, k = 0; i < nums.length; i += 2) {
87-
for (int j = 0; j < nums[i - 1]; ++j) {
88-
res[k++] = nums[i];
84+
for (int j = 0; j < nums[i]; ++j) {
85+
ans.add(nums[i + 1]);
8986
}
9087
}
91-
return res;
88+
return ans.stream().mapToInt(i -> i).toArray();
9289
}
9390
}
9491
```
@@ -99,41 +96,39 @@ class Solution {
9996
class Solution {
10097
public:
10198
vector<int> decompressRLElist(vector<int>& nums) {
102-
vector<int> res;
103-
for (int i = 1; i < nums.size(); i += 2) {
104-
for (int j = 0; j < nums[i - 1]; ++j) {
105-
res.push_back(nums[i]);
99+
vector<int> ans;
100+
for (int i = 0; i < nums.size(); i += 2) {
101+
for (int j = 0; j < nums[i]; j++) {
102+
ans.push_back(nums[i + 1]);
106103
}
107104
}
108-
return res;
105+
return ans;
109106
}
110107
};
111108
```
112109
113110
#### Go
114111
115112
```go
116-
func decompressRLElist(nums []int) []int {
117-
var res []int
113+
func decompressRLElist(nums []int) (ans []int) {
118114
for i := 1; i < len(nums); i += 2 {
119115
for j := 0; j < nums[i-1]; j++ {
120-
res = append(res, nums[i])
116+
ans = append(ans, nums[i])
121117
}
122118
}
123-
return res
119+
return
124120
}
125121
```
126122

127123
#### TypeScript
128124

129125
```ts
130126
function decompressRLElist(nums: number[]): number[] {
131-
let n = nums.length >> 1;
132-
let ans = [];
133-
for (let i = 0; i < n; i++) {
134-
let freq = nums[2 * i],
135-
val = nums[2 * i + 1];
136-
ans.push(...new Array(freq).fill(val));
127+
const ans: number[] = [];
128+
for (let i = 0; i < nums.length; i += 2) {
129+
for (let j = 0; j < nums[i]; j++) {
130+
ans.push(nums[i + 1]);
131+
}
137132
}
138133
return ans;
139134
}
@@ -144,12 +139,16 @@ function decompressRLElist(nums: number[]): number[] {
144139
```rust
145140
impl Solution {
146141
pub fn decompress_rl_elist(nums: Vec<i32>) -> Vec<i32> {
147-
let n = nums.len() >> 1;
148142
let mut ans = Vec::new();
149-
for i in 0..n {
150-
for _ in 0..nums[2 * i] {
151-
ans.push(nums[2 * i + 1]);
143+
let n = nums.len();
144+
let mut i = 0;
145+
while i < n {
146+
let freq = nums[i];
147+
let val = nums[i + 1];
148+
for _ in 0..freq {
149+
ans.push(val);
152150
}
151+
i += 2;
153152
}
154153
ans
155154
}
@@ -163,17 +162,20 @@ impl Solution {
163162
* Note: The returned array must be malloced, assume caller calls free().
164163
*/
165164
int* decompressRLElist(int* nums, int numsSize, int* returnSize) {
166-
int size = 0;
165+
int n = 0;
167166
for (int i = 0; i < numsSize; i += 2) {
168-
size += nums[i];
167+
n += nums[i];
169168
}
170-
int* ans = malloc(size * sizeof(int));
171-
for (int i = 0, j = 0; j < numsSize; j += 2) {
172-
for (int k = 0; k < nums[j]; k++) {
173-
ans[i++] = nums[j + 1];
169+
int* ans = (int*) malloc(n * sizeof(int));
170+
*returnSize = n;
171+
int k = 0;
172+
for (int i = 0; i < numsSize; i += 2) {
173+
int freq = nums[i];
174+
int val = nums[i + 1];
175+
for (int j = 0; j < freq; j++) {
176+
ans[k++] = val;
174177
}
175178
}
176-
*returnSize = size;
177179
return ans;
178180
}
179181
```

solution/1300-1399/1313.Decompress Run-Length Encoded List/README_EN.md

+41-39
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ At the end the concatenation [2] + [4,4,4] is [2,4,4,4].
5757

5858
<!-- solution:start -->
5959

60-
### Solution 1
60+
### Solution 1: Simulation
61+
62+
We can directly simulate the process described in the problem. Traverse the array $\textit{nums}$ from left to right, each time taking out two numbers $\textit{freq}$ and $\textit{val}$, then repeat $\textit{val}$ $\textit{freq}$ times, and add these $\textit{freq}$ $\textit{val}$s to the answer array.
63+
64+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. We only need to traverse the array $\textit{nums}$ once. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.
6165

6266
<!-- tabs:start -->
6367

@@ -66,28 +70,21 @@ At the end the concatenation [2] + [4,4,4] is [2,4,4,4].
6670
```python
6771
class Solution:
6872
def decompressRLElist(self, nums: List[int]) -> List[int]:
69-
res = []
70-
for i in range(1, len(nums), 2):
71-
res.extend([nums[i]] * nums[i - 1])
72-
return res
73+
return [nums[i + 1] for i in range(0, len(nums), 2) for _ in range(nums[i])]
7374
```
7475

7576
#### Java
7677

7778
```java
7879
class Solution {
7980
public int[] decompressRLElist(int[] nums) {
80-
int n = 0;
81+
List<Integer> ans = new ArrayList<>();
8182
for (int i = 0; i < nums.length; i += 2) {
82-
n += nums[i];
83-
}
84-
int[] res = new int[n];
85-
for (int i = 1, k = 0; i < nums.length; i += 2) {
86-
for (int j = 0; j < nums[i - 1]; ++j) {
87-
res[k++] = nums[i];
83+
for (int j = 0; j < nums[i]; ++j) {
84+
ans.add(nums[i + 1]);
8885
}
8986
}
90-
return res;
87+
return ans.stream().mapToInt(i -> i).toArray();
9188
}
9289
}
9390
```
@@ -98,41 +95,39 @@ class Solution {
9895
class Solution {
9996
public:
10097
vector<int> decompressRLElist(vector<int>& nums) {
101-
vector<int> res;
102-
for (int i = 1; i < nums.size(); i += 2) {
103-
for (int j = 0; j < nums[i - 1]; ++j) {
104-
res.push_back(nums[i]);
98+
vector<int> ans;
99+
for (int i = 0; i < nums.size(); i += 2) {
100+
for (int j = 0; j < nums[i]; j++) {
101+
ans.push_back(nums[i + 1]);
105102
}
106103
}
107-
return res;
104+
return ans;
108105
}
109106
};
110107
```
111108
112109
#### Go
113110
114111
```go
115-
func decompressRLElist(nums []int) []int {
116-
var res []int
112+
func decompressRLElist(nums []int) (ans []int) {
117113
for i := 1; i < len(nums); i += 2 {
118114
for j := 0; j < nums[i-1]; j++ {
119-
res = append(res, nums[i])
115+
ans = append(ans, nums[i])
120116
}
121117
}
122-
return res
118+
return
123119
}
124120
```
125121

126122
#### TypeScript
127123

128124
```ts
129125
function decompressRLElist(nums: number[]): number[] {
130-
let n = nums.length >> 1;
131-
let ans = [];
132-
for (let i = 0; i < n; i++) {
133-
let freq = nums[2 * i],
134-
val = nums[2 * i + 1];
135-
ans.push(...new Array(freq).fill(val));
126+
const ans: number[] = [];
127+
for (let i = 0; i < nums.length; i += 2) {
128+
for (let j = 0; j < nums[i]; j++) {
129+
ans.push(nums[i + 1]);
130+
}
136131
}
137132
return ans;
138133
}
@@ -143,12 +138,16 @@ function decompressRLElist(nums: number[]): number[] {
143138
```rust
144139
impl Solution {
145140
pub fn decompress_rl_elist(nums: Vec<i32>) -> Vec<i32> {
146-
let n = nums.len() >> 1;
147141
let mut ans = Vec::new();
148-
for i in 0..n {
149-
for _ in 0..nums[2 * i] {
150-
ans.push(nums[2 * i + 1]);
142+
let n = nums.len();
143+
let mut i = 0;
144+
while i < n {
145+
let freq = nums[i];
146+
let val = nums[i + 1];
147+
for _ in 0..freq {
148+
ans.push(val);
151149
}
150+
i += 2;
152151
}
153152
ans
154153
}
@@ -162,17 +161,20 @@ impl Solution {
162161
* Note: The returned array must be malloced, assume caller calls free().
163162
*/
164163
int* decompressRLElist(int* nums, int numsSize, int* returnSize) {
165-
int size = 0;
164+
int n = 0;
166165
for (int i = 0; i < numsSize; i += 2) {
167-
size += nums[i];
166+
n += nums[i];
168167
}
169-
int* ans = malloc(size * sizeof(int));
170-
for (int i = 0, j = 0; j < numsSize; j += 2) {
171-
for (int k = 0; k < nums[j]; k++) {
172-
ans[i++] = nums[j + 1];
168+
int* ans = (int*) malloc(n * sizeof(int));
169+
*returnSize = n;
170+
int k = 0;
171+
for (int i = 0; i < numsSize; i += 2) {
172+
int freq = nums[i];
173+
int val = nums[i + 1];
174+
for (int j = 0; j < freq; j++) {
175+
ans[k++] = val;
173176
}
174177
}
175-
*returnSize = size;
176178
return ans;
177179
}
178180
```

solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.c

+11-8
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
* Note: The returned array must be malloced, assume caller calls free().
33
*/
44
int* decompressRLElist(int* nums, int numsSize, int* returnSize) {
5-
int size = 0;
5+
int n = 0;
66
for (int i = 0; i < numsSize; i += 2) {
7-
size += nums[i];
7+
n += nums[i];
88
}
9-
int* ans = malloc(size * sizeof(int));
10-
for (int i = 0, j = 0; j < numsSize; j += 2) {
11-
for (int k = 0; k < nums[j]; k++) {
12-
ans[i++] = nums[j + 1];
9+
int* ans = (int*) malloc(n * sizeof(int));
10+
*returnSize = n;
11+
int k = 0;
12+
for (int i = 0; i < numsSize; i += 2) {
13+
int freq = nums[i];
14+
int val = nums[i + 1];
15+
for (int j = 0; j < freq; j++) {
16+
ans[k++] = val;
1317
}
1418
}
15-
*returnSize = size;
1619
return ans;
17-
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public:
33
vector<int> decompressRLElist(vector<int>& nums) {
4-
vector<int> res;
5-
for (int i = 1; i < nums.size(); i += 2) {
6-
for (int j = 0; j < nums[i - 1]; ++j) {
7-
res.push_back(nums[i]);
4+
vector<int> ans;
5+
for (int i = 0; i < nums.size(); i += 2) {
6+
for (int j = 0; j < nums[i]; j++) {
7+
ans.push_back(nums[i + 1]);
88
}
99
}
10-
return res;
10+
return ans;
1111
}
12-
};
12+
};
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
func decompressRLElist(nums []int) []int {
2-
var res []int
1+
func decompressRLElist(nums []int) (ans []int) {
32
for i := 1; i < len(nums); i += 2 {
43
for j := 0; j < nums[i-1]; j++ {
5-
res = append(res, nums[i])
4+
ans = append(ans, nums[i])
65
}
76
}
8-
return res
9-
}
7+
return
8+
}

0 commit comments

Comments
 (0)