From 0c0e25a0348aa9c6f830cce1da9c13d115d5f0ad Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 4 Sep 2024 20:10:28 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1313 No.1313.Decompress Run-Length Encoded List --- .gitignore | 3 +- .../README.md | 80 ++++++++++--------- .../README_EN.md | 80 ++++++++++--------- .../Solution.c | 19 +++-- .../Solution.cpp | 12 +-- .../Solution.go | 9 +-- .../Solution.java | 14 ++-- .../Solution.py | 5 +- .../Solution.rs | 12 ++- .../Solution.ts | 11 ++- 10 files changed, 124 insertions(+), 121 deletions(-) diff --git a/.gitignore b/.gitignore index 474929a437363..e8ebbedd5ad1e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,10 +2,11 @@ .DS_Store .vscode .temp +.vitepress .cache *.iml __pycache__ /node_modules /solution/result.json /solution/__pycache__ -/solution/.env \ No newline at end of file +/solution/.env diff --git a/solution/1300-1399/1313.Decompress Run-Length Encoded List/README.md b/solution/1300-1399/1313.Decompress Run-Length Encoded List/README.md index 91587275b4dae..9219d56ccc8a8 100644 --- a/solution/1300-1399/1313.Decompress Run-Length Encoded List/README.md +++ b/solution/1300-1399/1313.Decompress Run-Length Encoded List/README.md @@ -58,7 +58,11 @@ tags: -### 方法一 +### 方法一:模拟 + +我们可以直接模拟题目描述的过程,从左到右遍历数组 $\textit{nums}$,每次取出两个数 $\textit{freq}$ 和 $\textit{val}$,然后将 $\textit{val}$ 重复 $\textit{freq}$ 次,将这 $\textit{freq}$ 个 $\textit{val}$ 加入答案数组即可。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。我们只需要遍历一次数组 $\textit{nums}$ 即可。忽略答案数组的空间消耗,空间复杂度 $O(1)$。 @@ -67,10 +71,7 @@ tags: ```python class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: - res = [] - for i in range(1, len(nums), 2): - res.extend([nums[i]] * nums[i - 1]) - return res + return [nums[i + 1] for i in range(0, len(nums), 2) for _ in range(nums[i])] ``` #### Java @@ -78,17 +79,13 @@ class Solution: ```java class Solution { public int[] decompressRLElist(int[] nums) { - int n = 0; + List ans = new ArrayList<>(); for (int i = 0; i < nums.length; i += 2) { - n += nums[i]; - } - int[] res = new int[n]; - for (int i = 1, k = 0; i < nums.length; i += 2) { - for (int j = 0; j < nums[i - 1]; ++j) { - res[k++] = nums[i]; + for (int j = 0; j < nums[i]; ++j) { + ans.add(nums[i + 1]); } } - return res; + return ans.stream().mapToInt(i -> i).toArray(); } } ``` @@ -99,13 +96,13 @@ class Solution { class Solution { public: vector decompressRLElist(vector& nums) { - vector res; - for (int i = 1; i < nums.size(); i += 2) { - for (int j = 0; j < nums[i - 1]; ++j) { - res.push_back(nums[i]); + vector ans; + for (int i = 0; i < nums.size(); i += 2) { + for (int j = 0; j < nums[i]; j++) { + ans.push_back(nums[i + 1]); } } - return res; + return ans; } }; ``` @@ -113,14 +110,13 @@ public: #### Go ```go -func decompressRLElist(nums []int) []int { - var res []int +func decompressRLElist(nums []int) (ans []int) { for i := 1; i < len(nums); i += 2 { for j := 0; j < nums[i-1]; j++ { - res = append(res, nums[i]) + ans = append(ans, nums[i]) } } - return res + return } ``` @@ -128,12 +124,11 @@ func decompressRLElist(nums []int) []int { ```ts function decompressRLElist(nums: number[]): number[] { - let n = nums.length >> 1; - let ans = []; - for (let i = 0; i < n; i++) { - let freq = nums[2 * i], - val = nums[2 * i + 1]; - ans.push(...new Array(freq).fill(val)); + const ans: number[] = []; + for (let i = 0; i < nums.length; i += 2) { + for (let j = 0; j < nums[i]; j++) { + ans.push(nums[i + 1]); + } } return ans; } @@ -144,12 +139,16 @@ function decompressRLElist(nums: number[]): number[] { ```rust impl Solution { pub fn decompress_rl_elist(nums: Vec) -> Vec { - let n = nums.len() >> 1; let mut ans = Vec::new(); - for i in 0..n { - for _ in 0..nums[2 * i] { - ans.push(nums[2 * i + 1]); + let n = nums.len(); + let mut i = 0; + while i < n { + let freq = nums[i]; + let val = nums[i + 1]; + for _ in 0..freq { + ans.push(val); } + i += 2; } ans } @@ -163,17 +162,20 @@ impl Solution { * Note: The returned array must be malloced, assume caller calls free(). */ int* decompressRLElist(int* nums, int numsSize, int* returnSize) { - int size = 0; + int n = 0; for (int i = 0; i < numsSize; i += 2) { - size += nums[i]; + n += nums[i]; } - int* ans = malloc(size * sizeof(int)); - for (int i = 0, j = 0; j < numsSize; j += 2) { - for (int k = 0; k < nums[j]; k++) { - ans[i++] = nums[j + 1]; + int* ans = (int*) malloc(n * sizeof(int)); + *returnSize = n; + int k = 0; + for (int i = 0; i < numsSize; i += 2) { + int freq = nums[i]; + int val = nums[i + 1]; + for (int j = 0; j < freq; j++) { + ans[k++] = val; } } - *returnSize = size; return ans; } ``` diff --git a/solution/1300-1399/1313.Decompress Run-Length Encoded List/README_EN.md b/solution/1300-1399/1313.Decompress Run-Length Encoded List/README_EN.md index b27e911e56a5f..eb3f62bfea9d4 100644 --- a/solution/1300-1399/1313.Decompress Run-Length Encoded List/README_EN.md +++ b/solution/1300-1399/1313.Decompress Run-Length Encoded List/README_EN.md @@ -57,7 +57,11 @@ At the end the concatenation [2] + [4,4,4] is [2,4,4,4]. -### Solution 1 +### Solution 1: Simulation + +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. + +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)$. @@ -66,10 +70,7 @@ At the end the concatenation [2] + [4,4,4] is [2,4,4,4]. ```python class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: - res = [] - for i in range(1, len(nums), 2): - res.extend([nums[i]] * nums[i - 1]) - return res + return [nums[i + 1] for i in range(0, len(nums), 2) for _ in range(nums[i])] ``` #### Java @@ -77,17 +78,13 @@ class Solution: ```java class Solution { public int[] decompressRLElist(int[] nums) { - int n = 0; + List ans = new ArrayList<>(); for (int i = 0; i < nums.length; i += 2) { - n += nums[i]; - } - int[] res = new int[n]; - for (int i = 1, k = 0; i < nums.length; i += 2) { - for (int j = 0; j < nums[i - 1]; ++j) { - res[k++] = nums[i]; + for (int j = 0; j < nums[i]; ++j) { + ans.add(nums[i + 1]); } } - return res; + return ans.stream().mapToInt(i -> i).toArray(); } } ``` @@ -98,13 +95,13 @@ class Solution { class Solution { public: vector decompressRLElist(vector& nums) { - vector res; - for (int i = 1; i < nums.size(); i += 2) { - for (int j = 0; j < nums[i - 1]; ++j) { - res.push_back(nums[i]); + vector ans; + for (int i = 0; i < nums.size(); i += 2) { + for (int j = 0; j < nums[i]; j++) { + ans.push_back(nums[i + 1]); } } - return res; + return ans; } }; ``` @@ -112,14 +109,13 @@ public: #### Go ```go -func decompressRLElist(nums []int) []int { - var res []int +func decompressRLElist(nums []int) (ans []int) { for i := 1; i < len(nums); i += 2 { for j := 0; j < nums[i-1]; j++ { - res = append(res, nums[i]) + ans = append(ans, nums[i]) } } - return res + return } ``` @@ -127,12 +123,11 @@ func decompressRLElist(nums []int) []int { ```ts function decompressRLElist(nums: number[]): number[] { - let n = nums.length >> 1; - let ans = []; - for (let i = 0; i < n; i++) { - let freq = nums[2 * i], - val = nums[2 * i + 1]; - ans.push(...new Array(freq).fill(val)); + const ans: number[] = []; + for (let i = 0; i < nums.length; i += 2) { + for (let j = 0; j < nums[i]; j++) { + ans.push(nums[i + 1]); + } } return ans; } @@ -143,12 +138,16 @@ function decompressRLElist(nums: number[]): number[] { ```rust impl Solution { pub fn decompress_rl_elist(nums: Vec) -> Vec { - let n = nums.len() >> 1; let mut ans = Vec::new(); - for i in 0..n { - for _ in 0..nums[2 * i] { - ans.push(nums[2 * i + 1]); + let n = nums.len(); + let mut i = 0; + while i < n { + let freq = nums[i]; + let val = nums[i + 1]; + for _ in 0..freq { + ans.push(val); } + i += 2; } ans } @@ -162,17 +161,20 @@ impl Solution { * Note: The returned array must be malloced, assume caller calls free(). */ int* decompressRLElist(int* nums, int numsSize, int* returnSize) { - int size = 0; + int n = 0; for (int i = 0; i < numsSize; i += 2) { - size += nums[i]; + n += nums[i]; } - int* ans = malloc(size * sizeof(int)); - for (int i = 0, j = 0; j < numsSize; j += 2) { - for (int k = 0; k < nums[j]; k++) { - ans[i++] = nums[j + 1]; + int* ans = (int*) malloc(n * sizeof(int)); + *returnSize = n; + int k = 0; + for (int i = 0; i < numsSize; i += 2) { + int freq = nums[i]; + int val = nums[i + 1]; + for (int j = 0; j < freq; j++) { + ans[k++] = val; } } - *returnSize = size; return ans; } ``` diff --git a/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.c b/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.c index 7e0d38c1cd1ab..4bd856b517d24 100644 --- a/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.c +++ b/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.c @@ -2,16 +2,19 @@ * Note: The returned array must be malloced, assume caller calls free(). */ int* decompressRLElist(int* nums, int numsSize, int* returnSize) { - int size = 0; + int n = 0; for (int i = 0; i < numsSize; i += 2) { - size += nums[i]; + n += nums[i]; } - int* ans = malloc(size * sizeof(int)); - for (int i = 0, j = 0; j < numsSize; j += 2) { - for (int k = 0; k < nums[j]; k++) { - ans[i++] = nums[j + 1]; + int* ans = (int*) malloc(n * sizeof(int)); + *returnSize = n; + int k = 0; + for (int i = 0; i < numsSize; i += 2) { + int freq = nums[i]; + int val = nums[i + 1]; + for (int j = 0; j < freq; j++) { + ans[k++] = val; } } - *returnSize = size; return ans; -} \ No newline at end of file +} diff --git a/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.cpp b/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.cpp index f8c31ec682773..76160be343def 100644 --- a/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.cpp +++ b/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.cpp @@ -1,12 +1,12 @@ class Solution { public: vector decompressRLElist(vector& nums) { - vector res; - for (int i = 1; i < nums.size(); i += 2) { - for (int j = 0; j < nums[i - 1]; ++j) { - res.push_back(nums[i]); + vector ans; + for (int i = 0; i < nums.size(); i += 2) { + for (int j = 0; j < nums[i]; j++) { + ans.push_back(nums[i + 1]); } } - return res; + return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.go b/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.go index 4390dfc76a15a..e762c6a032d66 100644 --- a/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.go +++ b/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.go @@ -1,9 +1,8 @@ -func decompressRLElist(nums []int) []int { - var res []int +func decompressRLElist(nums []int) (ans []int) { for i := 1; i < len(nums); i += 2 { for j := 0; j < nums[i-1]; j++ { - res = append(res, nums[i]) + ans = append(ans, nums[i]) } } - return res -} \ No newline at end of file + return +} diff --git a/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.java b/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.java index 1cf33204af29f..86164892464b8 100644 --- a/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.java +++ b/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.java @@ -1,15 +1,11 @@ class Solution { public int[] decompressRLElist(int[] nums) { - int n = 0; + List ans = new ArrayList<>(); for (int i = 0; i < nums.length; i += 2) { - n += nums[i]; - } - int[] res = new int[n]; - for (int i = 1, k = 0; i < nums.length; i += 2) { - for (int j = 0; j < nums[i - 1]; ++j) { - res[k++] = nums[i]; + for (int j = 0; j < nums[i]; ++j) { + ans.add(nums[i + 1]); } } - return res; + return ans.stream().mapToInt(i -> i).toArray(); } -} \ No newline at end of file +} diff --git a/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.py b/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.py index d364e6382bf08..3ab3d7d78425a 100644 --- a/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.py +++ b/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.py @@ -1,6 +1,3 @@ class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: - res = [] - for i in range(1, len(nums), 2): - res.extend([nums[i]] * nums[i - 1]) - return res + return [nums[i + 1] for i in range(0, len(nums), 2) for _ in range(nums[i])] diff --git a/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.rs b/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.rs index 904ebd1eba1bd..56dd1b43a8349 100644 --- a/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.rs +++ b/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.rs @@ -1,11 +1,15 @@ impl Solution { pub fn decompress_rl_elist(nums: Vec) -> Vec { - let n = nums.len() >> 1; let mut ans = Vec::new(); - for i in 0..n { - for _ in 0..nums[2 * i] { - ans.push(nums[2 * i + 1]); + let n = nums.len(); + let mut i = 0; + while i < n { + let freq = nums[i]; + let val = nums[i + 1]; + for _ in 0..freq { + ans.push(val); } + i += 2; } ans } diff --git a/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.ts b/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.ts index 9228fe66564fa..bea4042cafbc1 100644 --- a/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.ts +++ b/solution/1300-1399/1313.Decompress Run-Length Encoded List/Solution.ts @@ -1,10 +1,9 @@ function decompressRLElist(nums: number[]): number[] { - let n = nums.length >> 1; - let ans = []; - for (let i = 0; i < n; i++) { - let freq = nums[2 * i], - val = nums[2 * i + 1]; - ans.push(...new Array(freq).fill(val)); + const ans: number[] = []; + for (let i = 0; i < nums.length; i += 2) { + for (let j = 0; j < nums[i]; j++) { + ans.push(nums[i + 1]); + } } return ans; }