From b594ce7d35b76e698664b1a9806c7b7b8a7267b7 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 11 May 2024 17:27:46 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.2191,2194 * No.2191.Sort the Jumbled Numbers * No.2194.Cells in a Range on an Excel Sheet --- .../2191.Sort the Jumbled Numbers/README.md | 188 ++++++++++++----- .../README_EN.md | 194 +++++++++++++----- .../Solution.cpp | 25 ++- .../2191.Sort the Jumbled Numbers/Solution.cs | 32 +++ .../2191.Sort the Jumbled Numbers/Solution.go | 27 ++- .../Solution.java | 27 ++- .../2191.Sort the Jumbled Numbers/Solution.py | 16 +- .../2191.Sort the Jumbled Numbers/Solution.rs | 32 +++ .../2191.Sort the Jumbled Numbers/Solution.ts | 23 ++- .../README.md | 29 ++- .../README_EN.md | 29 ++- .../Solution.cpp | 6 +- .../Solution.go | 5 +- .../Solution.ts | 9 + 14 files changed, 474 insertions(+), 168 deletions(-) create mode 100644 solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.cs create mode 100644 solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.rs create mode 100644 solution/2100-2199/2194.Cells in a Range on an Excel Sheet/Solution.ts diff --git a/solution/2100-2199/2191.Sort the Jumbled Numbers/README.md b/solution/2100-2199/2191.Sort the Jumbled Numbers/README.md index 2c181f10dac21..e0a44222c36c8 100644 --- a/solution/2100-2199/2191.Sort the Jumbled Numbers/README.md +++ b/solution/2100-2199/2191.Sort the Jumbled Numbers/README.md @@ -72,33 +72,31 @@ ```python class Solution: def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]: - arr = [] - for i, x in enumerate(nums): - y = mapping[0] if x == 0 else 0 - k = 1 + def f(x: int) -> int: + if x == 0: + return mapping[0] + y, k = 0, 1 while x: x, v = divmod(x, 10) - y = mapping[v] * k + y + v = mapping[v] + y = k * v + y k *= 10 - arr.append((y, i)) - arr.sort() + return y + + arr = sorted((f(x), i) for i, x in enumerate(nums)) return [nums[i] for _, i in arr] ``` ```java class Solution { + private int[] mapping; + public int[] sortJumbled(int[] mapping, int[] nums) { + this.mapping = mapping; int n = nums.length; - int[][] arr = new int[n][2]; + int[][] arr = new int[n][0]; for (int i = 0; i < n; ++i) { - int x = nums[i]; - int y = x == 0 ? mapping[0] : 0; - int k = 1; - for (; x > 0; x /= 10) { - y += k * mapping[x % 10]; - k *= 10; - } - arr[i] = new int[] {y, i}; + arr[i] = new int[] {f(nums[i]), i}; } Arrays.sort(arr, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]); int[] ans = new int[n]; @@ -107,6 +105,19 @@ class Solution { } return ans; } + + private int f(int x) { + if (x == 0) { + return mapping[0]; + } + int y = 0; + for (int k = 1; x > 0; x /= 10) { + int v = mapping[x % 10]; + y = k * v + y; + k *= 10; + } + return y; + } } ``` @@ -114,21 +125,26 @@ class Solution { class Solution { public: vector sortJumbled(vector& mapping, vector& nums) { - int n = nums.size(); - vector> arr(n); - for (int i = 0; i < n; ++i) { - int x = nums[i]; - int y = x == 0 ? mapping[0] : 0; - int k = 1; - for (; x; x /= 10) { - y += k * mapping[x % 10]; + auto f = [&](int x) { + if (x == 0) { + return mapping[0]; + } + int y = 0; + for (int k = 1; x; x /= 10) { + int v = mapping[x % 10]; + y = k * v + y; k *= 10; } - arr[i] = {y, i}; + return y; + }; + const int n = nums.size(); + vector> arr; + for (int i = 0; i < n; ++i) { + arr.emplace_back(f(nums[i]), i); } sort(arr.begin(), arr.end()); vector ans; - for (auto& [_, i] : arr) { + for (const auto& [_, i] : arr) { ans.push_back(nums[i]); } return ans; @@ -139,25 +155,24 @@ public: ```go func sortJumbled(mapping []int, nums []int) (ans []int) { n := len(nums) - arr := make([][2]int, n) - for i, x := range nums { - y := 0 + f := func(x int) (y int) { if x == 0 { - y = mapping[0] + return mapping[0] } - k := 1 - for ; x > 0; x /= 10 { - y += k * mapping[x%10] + for k := 1; x > 0; x /= 10 { + v := mapping[x%10] + y = k*v + y k *= 10 } - arr[i] = [2]int{y, i} + return + } + arr := make([][2]int, n) + for i, x := range nums { + arr[i] = [2]int{f(x), i} } - sort.Slice(arr, func(i, j int) bool { - a, b := arr[i], arr[j] - return a[0] < b[0] || a[0] == b[0] && a[1] < b[1] - }) - for _, x := range arr { - ans = append(ans, nums[x[1]]) + sort.Slice(arr, func(i, j int) bool { return arr[i][0] < arr[j][0] || arr[i][0] == arr[j][0] && arr[i][1] < arr[j][1] }) + for _, p := range arr { + ans = append(ans, nums[p[1]]) } return } @@ -166,18 +181,91 @@ func sortJumbled(mapping []int, nums []int) (ans []int) { ```ts function sortJumbled(mapping: number[], nums: number[]): number[] { const n = nums.length; - const arr: number[][] = []; - for (let i = 0; i < n; ++i) { - let x = nums[i]; - let y = x === 0 ? mapping[0] : 0; - let k = 1; - for (; x > 0; x = Math.floor(x / 10), k *= 10) { - y += mapping[x % 10] * k; + const f = (x: number): number => { + if (x === 0) { + return mapping[0]; } - arr.push([y, i]); - } + let y = 0; + for (let k = 1; x; x = (x / 10) | 0) { + const v = mapping[x % 10]; + y += v * k; + k *= 10; + } + return y; + }; + const arr: number[][] = nums.map((x, i) => [f(x), i]); arr.sort((a, b) => (a[0] === b[0] ? a[1] - b[1] : a[0] - b[0])); - return arr.map(a => nums[a[1]]); + return arr.map(x => nums[x[1]]); +} +``` + +```rust +impl Solution { + pub fn sort_jumbled(mapping: Vec, nums: Vec) -> Vec { + let f = |x: i32| -> i32 { + if x == 0 { + return mapping[0]; + } + let mut y = 0; + let mut k = 1; + let mut num = x; + while num != 0 { + let v = mapping[(num % 10) as usize]; + y = k * v + y; + k *= 10; + num /= 10; + } + y + }; + + let n = nums.len(); + let mut arr: Vec<(i32, usize)> = Vec::with_capacity(n); + for i in 0..n { + arr.push((f(nums[i]), i)); + } + arr.sort(); + + let mut ans: Vec = Vec::with_capacity(n); + for (_, i) in arr { + ans.push(nums[i]); + } + ans + } +} +``` + +```cs +public class Solution { + public int[] SortJumbled(int[] mapping, int[] nums) { + Func f = (int x) => { + if (x == 0) { + return mapping[0]; + } + int y = 0; + int k = 1; + int num = x; + while (num != 0) { + int v = mapping[num % 10]; + y = k * v + y; + k *= 10; + num /= 10; + } + return y; + }; + + int n = nums.Length; + List<(int, int)> arr = new List<(int, int)>(); + for (int i = 0; i < n; ++i) { + arr.Add((f(nums[i]), i)); + } + arr.Sort(); + + int[] ans = new int[n]; + for (int i = 0; i < n; ++i) { + ans[i] = nums[arr[i].Item2]; + } + return ans; + } } ``` diff --git a/solution/2100-2199/2191.Sort the Jumbled Numbers/README_EN.md b/solution/2100-2199/2191.Sort the Jumbled Numbers/README_EN.md index 414d4b6b1c3e1..0f9a1255e4485 100644 --- a/solution/2100-2199/2191.Sort the Jumbled Numbers/README_EN.md +++ b/solution/2100-2199/2191.Sort the Jumbled Numbers/README_EN.md @@ -57,40 +57,42 @@ Thus, the sorted array is [338,38,991]. ## Solutions -### Solution 1 +### Solution 1: Custom Sorting + +We traverse each element $nums[i]$ in the array $nums$, store its mapped value $y$ and index $i$ into the array $arr$, then sort the array $arr$. Finally, we extract the index $i$ from the sorted array $arr$, convert it to the element $nums[i]$ in the original array $nums$. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $nums$. ```python class Solution: def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]: - arr = [] - for i, x in enumerate(nums): - y = mapping[0] if x == 0 else 0 - k = 1 + def f(x: int) -> int: + if x == 0: + return mapping[0] + y, k = 0, 1 while x: x, v = divmod(x, 10) - y = mapping[v] * k + y + v = mapping[v] + y = k * v + y k *= 10 - arr.append((y, i)) - arr.sort() + return y + + arr = sorted((f(x), i) for i, x in enumerate(nums)) return [nums[i] for _, i in arr] ``` ```java class Solution { + private int[] mapping; + public int[] sortJumbled(int[] mapping, int[] nums) { + this.mapping = mapping; int n = nums.length; - int[][] arr = new int[n][2]; + int[][] arr = new int[n][0]; for (int i = 0; i < n; ++i) { - int x = nums[i]; - int y = x == 0 ? mapping[0] : 0; - int k = 1; - for (; x > 0; x /= 10) { - y += k * mapping[x % 10]; - k *= 10; - } - arr[i] = new int[] {y, i}; + arr[i] = new int[] {f(nums[i]), i}; } Arrays.sort(arr, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]); int[] ans = new int[n]; @@ -99,6 +101,19 @@ class Solution { } return ans; } + + private int f(int x) { + if (x == 0) { + return mapping[0]; + } + int y = 0; + for (int k = 1; x > 0; x /= 10) { + int v = mapping[x % 10]; + y = k * v + y; + k *= 10; + } + return y; + } } ``` @@ -106,21 +121,26 @@ class Solution { class Solution { public: vector sortJumbled(vector& mapping, vector& nums) { - int n = nums.size(); - vector> arr(n); - for (int i = 0; i < n; ++i) { - int x = nums[i]; - int y = x == 0 ? mapping[0] : 0; - int k = 1; - for (; x; x /= 10) { - y += k * mapping[x % 10]; + auto f = [&](int x) { + if (x == 0) { + return mapping[0]; + } + int y = 0; + for (int k = 1; x; x /= 10) { + int v = mapping[x % 10]; + y = k * v + y; k *= 10; } - arr[i] = {y, i}; + return y; + }; + const int n = nums.size(); + vector> arr; + for (int i = 0; i < n; ++i) { + arr.emplace_back(f(nums[i]), i); } sort(arr.begin(), arr.end()); vector ans; - for (auto& [_, i] : arr) { + for (const auto& [_, i] : arr) { ans.push_back(nums[i]); } return ans; @@ -131,25 +151,24 @@ public: ```go func sortJumbled(mapping []int, nums []int) (ans []int) { n := len(nums) - arr := make([][2]int, n) - for i, x := range nums { - y := 0 + f := func(x int) (y int) { if x == 0 { - y = mapping[0] + return mapping[0] } - k := 1 - for ; x > 0; x /= 10 { - y += k * mapping[x%10] + for k := 1; x > 0; x /= 10 { + v := mapping[x%10] + y = k*v + y k *= 10 } - arr[i] = [2]int{y, i} + return + } + arr := make([][2]int, n) + for i, x := range nums { + arr[i] = [2]int{f(x), i} } - sort.Slice(arr, func(i, j int) bool { - a, b := arr[i], arr[j] - return a[0] < b[0] || a[0] == b[0] && a[1] < b[1] - }) - for _, x := range arr { - ans = append(ans, nums[x[1]]) + sort.Slice(arr, func(i, j int) bool { return arr[i][0] < arr[j][0] || arr[i][0] == arr[j][0] && arr[i][1] < arr[j][1] }) + for _, p := range arr { + ans = append(ans, nums[p[1]]) } return } @@ -158,18 +177,91 @@ func sortJumbled(mapping []int, nums []int) (ans []int) { ```ts function sortJumbled(mapping: number[], nums: number[]): number[] { const n = nums.length; - const arr: number[][] = []; - for (let i = 0; i < n; ++i) { - let x = nums[i]; - let y = x === 0 ? mapping[0] : 0; - let k = 1; - for (; x > 0; x = Math.floor(x / 10), k *= 10) { - y += mapping[x % 10] * k; + const f = (x: number): number => { + if (x === 0) { + return mapping[0]; } - arr.push([y, i]); - } + let y = 0; + for (let k = 1; x; x = (x / 10) | 0) { + const v = mapping[x % 10]; + y += v * k; + k *= 10; + } + return y; + }; + const arr: number[][] = nums.map((x, i) => [f(x), i]); arr.sort((a, b) => (a[0] === b[0] ? a[1] - b[1] : a[0] - b[0])); - return arr.map(a => nums[a[1]]); + return arr.map(x => nums[x[1]]); +} +``` + +```rust +impl Solution { + pub fn sort_jumbled(mapping: Vec, nums: Vec) -> Vec { + let f = |x: i32| -> i32 { + if x == 0 { + return mapping[0]; + } + let mut y = 0; + let mut k = 1; + let mut num = x; + while num != 0 { + let v = mapping[(num % 10) as usize]; + y = k * v + y; + k *= 10; + num /= 10; + } + y + }; + + let n = nums.len(); + let mut arr: Vec<(i32, usize)> = Vec::with_capacity(n); + for i in 0..n { + arr.push((f(nums[i]), i)); + } + arr.sort(); + + let mut ans: Vec = Vec::with_capacity(n); + for (_, i) in arr { + ans.push(nums[i]); + } + ans + } +} +``` + +```cs +public class Solution { + public int[] SortJumbled(int[] mapping, int[] nums) { + Func f = (int x) => { + if (x == 0) { + return mapping[0]; + } + int y = 0; + int k = 1; + int num = x; + while (num != 0) { + int v = mapping[num % 10]; + y = k * v + y; + k *= 10; + num /= 10; + } + return y; + }; + + int n = nums.Length; + List<(int, int)> arr = new List<(int, int)>(); + for (int i = 0; i < n; ++i) { + arr.Add((f(nums[i]), i)); + } + arr.Sort(); + + int[] ans = new int[n]; + for (int i = 0; i < n; ++i) { + ans[i] = nums[arr[i].Item2]; + } + return ans; + } } ``` diff --git a/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.cpp b/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.cpp index 1dc2a59ef7644..42e40c1bdaeb2 100644 --- a/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.cpp +++ b/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.cpp @@ -1,21 +1,26 @@ class Solution { public: vector sortJumbled(vector& mapping, vector& nums) { - int n = nums.size(); - vector> arr(n); - for (int i = 0; i < n; ++i) { - int x = nums[i]; - int y = x == 0 ? mapping[0] : 0; - int k = 1; - for (; x; x /= 10) { - y += k * mapping[x % 10]; + auto f = [&](int x) { + if (x == 0) { + return mapping[0]; + } + int y = 0; + for (int k = 1; x; x /= 10) { + int v = mapping[x % 10]; + y = k * v + y; k *= 10; } - arr[i] = {y, i}; + return y; + }; + const int n = nums.size(); + vector> arr; + for (int i = 0; i < n; ++i) { + arr.emplace_back(f(nums[i]), i); } sort(arr.begin(), arr.end()); vector ans; - for (auto& [_, i] : arr) { + for (const auto& [_, i] : arr) { ans.push_back(nums[i]); } return ans; diff --git a/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.cs b/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.cs new file mode 100644 index 0000000000000..b4bf9bc574578 --- /dev/null +++ b/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.cs @@ -0,0 +1,32 @@ +public class Solution { + public int[] SortJumbled(int[] mapping, int[] nums) { + Func f = (int x) => { + if (x == 0) { + return mapping[0]; + } + int y = 0; + int k = 1; + int num = x; + while (num != 0) { + int v = mapping[num % 10]; + y = k * v + y; + k *= 10; + num /= 10; + } + return y; + }; + + int n = nums.Length; + List<(int, int)> arr = new List<(int, int)>(); + for (int i = 0; i < n; ++i) { + arr.Add((f(nums[i]), i)); + } + arr.Sort(); + + int[] ans = new int[n]; + for (int i = 0; i < n; ++i) { + ans[i] = nums[arr[i].Item2]; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.go b/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.go index f968aada58894..b807becb5ce00 100644 --- a/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.go +++ b/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.go @@ -1,24 +1,23 @@ func sortJumbled(mapping []int, nums []int) (ans []int) { n := len(nums) - arr := make([][2]int, n) - for i, x := range nums { - y := 0 + f := func(x int) (y int) { if x == 0 { - y = mapping[0] + return mapping[0] } - k := 1 - for ; x > 0; x /= 10 { - y += k * mapping[x%10] + for k := 1; x > 0; x /= 10 { + v := mapping[x%10] + y = k*v + y k *= 10 } - arr[i] = [2]int{y, i} + return + } + arr := make([][2]int, n) + for i, x := range nums { + arr[i] = [2]int{f(x), i} } - sort.Slice(arr, func(i, j int) bool { - a, b := arr[i], arr[j] - return a[0] < b[0] || a[0] == b[0] && a[1] < b[1] - }) - for _, x := range arr { - ans = append(ans, nums[x[1]]) + sort.Slice(arr, func(i, j int) bool { return arr[i][0] < arr[j][0] || arr[i][0] == arr[j][0] && arr[i][1] < arr[j][1] }) + for _, p := range arr { + ans = append(ans, nums[p[1]]) } return } \ No newline at end of file diff --git a/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.java b/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.java index 0f952e26c1138..7a930d7affc76 100644 --- a/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.java +++ b/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.java @@ -1,16 +1,12 @@ class Solution { + private int[] mapping; + public int[] sortJumbled(int[] mapping, int[] nums) { + this.mapping = mapping; int n = nums.length; - int[][] arr = new int[n][2]; + int[][] arr = new int[n][0]; for (int i = 0; i < n; ++i) { - int x = nums[i]; - int y = x == 0 ? mapping[0] : 0; - int k = 1; - for (; x > 0; x /= 10) { - y += k * mapping[x % 10]; - k *= 10; - } - arr[i] = new int[] {y, i}; + arr[i] = new int[] {f(nums[i]), i}; } Arrays.sort(arr, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]); int[] ans = new int[n]; @@ -19,4 +15,17 @@ public int[] sortJumbled(int[] mapping, int[] nums) { } return ans; } + + private int f(int x) { + if (x == 0) { + return mapping[0]; + } + int y = 0; + for (int k = 1; x > 0; x /= 10) { + int v = mapping[x % 10]; + y = k * v + y; + k *= 10; + } + return y; + } } \ No newline at end of file diff --git a/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.py b/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.py index 6fa7e1a84ea29..00a89308d9948 100644 --- a/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.py +++ b/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.py @@ -1,13 +1,15 @@ class Solution: def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]: - arr = [] - for i, x in enumerate(nums): - y = mapping[0] if x == 0 else 0 - k = 1 + def f(x: int) -> int: + if x == 0: + return mapping[0] + y, k = 0, 1 while x: x, v = divmod(x, 10) - y = mapping[v] * k + y + v = mapping[v] + y = k * v + y k *= 10 - arr.append((y, i)) - arr.sort() + return y + + arr = sorted((f(x), i) for i, x in enumerate(nums)) return [nums[i] for _, i in arr] diff --git a/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.rs b/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.rs new file mode 100644 index 0000000000000..d163cdc108ac0 --- /dev/null +++ b/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.rs @@ -0,0 +1,32 @@ +impl Solution { + pub fn sort_jumbled(mapping: Vec, nums: Vec) -> Vec { + let f = |x: i32| -> i32 { + if x == 0 { + return mapping[0]; + } + let mut y = 0; + let mut k = 1; + let mut num = x; + while num != 0 { + let v = mapping[(num % 10) as usize]; + y = k * v + y; + k *= 10; + num /= 10; + } + y + }; + + let n = nums.len(); + let mut arr: Vec<(i32, usize)> = Vec::with_capacity(n); + for i in 0..n { + arr.push((f(nums[i]), i)); + } + arr.sort(); + + let mut ans: Vec = Vec::with_capacity(n); + for (_, i) in arr { + ans.push(nums[i]); + } + ans + } +} diff --git a/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.ts b/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.ts index dffcae9d2fb1c..7a9ebbe9fa1ee 100644 --- a/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.ts +++ b/solution/2100-2199/2191.Sort the Jumbled Numbers/Solution.ts @@ -1,15 +1,18 @@ function sortJumbled(mapping: number[], nums: number[]): number[] { const n = nums.length; - const arr: number[][] = []; - for (let i = 0; i < n; ++i) { - let x = nums[i]; - let y = x === 0 ? mapping[0] : 0; - let k = 1; - for (; x > 0; x = Math.floor(x / 10), k *= 10) { - y += mapping[x % 10] * k; + const f = (x: number): number => { + if (x === 0) { + return mapping[0]; } - arr.push([y, i]); - } + let y = 0; + for (let k = 1; x; x = (x / 10) | 0) { + const v = mapping[x % 10]; + y += v * k; + k *= 10; + } + return y; + }; + const arr: number[][] = nums.map((x, i) => [f(x), i]); arr.sort((a, b) => (a[0] === b[0] ? a[1] - b[1] : a[0] - b[0])); - return arr.map(a => nums[a[1]]); + return arr.map(x => nums[x[1]]); } diff --git a/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/README.md b/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/README.md index ac161969b9a8a..93bc80d884444 100644 --- a/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/README.md +++ b/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/README.md @@ -64,7 +64,11 @@ ## 解法 -### 方法一 +### 方法一:模拟 + +我们直接遍历范围内的所有单元格,将其添加到答案数组中。 + +时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别为行数和列数的取值范围。 @@ -97,23 +101,36 @@ class Solution { public: vector cellsInRange(string s) { vector ans; - for (char i = s[0]; i <= s[3]; ++i) - for (char j = s[1]; j <= s[4]; ++j) + for (char i = s[0]; i <= s[3]; ++i) { + for (char j = s[1]; j <= s[4]; ++j) { ans.push_back({i, j}); + } + } return ans; } }; ``` ```go -func cellsInRange(s string) []string { - var ans []string +func cellsInRange(s string) (ans []string) { for i := s[0]; i <= s[3]; i++ { for j := s[1]; j <= s[4]; j++ { ans = append(ans, string(i)+string(j)) } } - return ans + return +} +``` + +```ts +function cellsInRange(s: string): string[] { + const ans: string[] = []; + for (let i = s.charCodeAt(0); i <= s.charCodeAt(3); ++i) { + for (let j = s.charCodeAt(1); j <= s.charCodeAt(4); ++j) { + ans.push(String.fromCharCode(i) + String.fromCharCode(j)); + } + } + return ans; } ``` diff --git a/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/README_EN.md b/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/README_EN.md index 6f2c6406cd1d8..98f5031c89f3b 100644 --- a/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/README_EN.md +++ b/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/README_EN.md @@ -56,7 +56,11 @@ The red arrow denotes the order in which the cells should be presented. ## Solutions -### Solution 1 +### Solution 1: Simulation + +We directly traverse all the cells within the range and add them to the answer array. + +The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$, where $m$ and $n$ are the range of rows and columns, respectively. @@ -89,23 +93,36 @@ class Solution { public: vector cellsInRange(string s) { vector ans; - for (char i = s[0]; i <= s[3]; ++i) - for (char j = s[1]; j <= s[4]; ++j) + for (char i = s[0]; i <= s[3]; ++i) { + for (char j = s[1]; j <= s[4]; ++j) { ans.push_back({i, j}); + } + } return ans; } }; ``` ```go -func cellsInRange(s string) []string { - var ans []string +func cellsInRange(s string) (ans []string) { for i := s[0]; i <= s[3]; i++ { for j := s[1]; j <= s[4]; j++ { ans = append(ans, string(i)+string(j)) } } - return ans + return +} +``` + +```ts +function cellsInRange(s: string): string[] { + const ans: string[] = []; + for (let i = s.charCodeAt(0); i <= s.charCodeAt(3); ++i) { + for (let j = s.charCodeAt(1); j <= s.charCodeAt(4); ++j) { + ans.push(String.fromCharCode(i) + String.fromCharCode(j)); + } + } + return ans; } ``` diff --git a/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/Solution.cpp b/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/Solution.cpp index dc47271f760cd..b9e17ecdeee7c 100644 --- a/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/Solution.cpp +++ b/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/Solution.cpp @@ -2,9 +2,11 @@ class Solution { public: vector cellsInRange(string s) { vector ans; - for (char i = s[0]; i <= s[3]; ++i) - for (char j = s[1]; j <= s[4]; ++j) + for (char i = s[0]; i <= s[3]; ++i) { + for (char j = s[1]; j <= s[4]; ++j) { ans.push_back({i, j}); + } + } return ans; } }; \ No newline at end of file diff --git a/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/Solution.go b/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/Solution.go index 4539bb685503b..deaf53529a478 100644 --- a/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/Solution.go +++ b/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/Solution.go @@ -1,9 +1,8 @@ -func cellsInRange(s string) []string { - var ans []string +func cellsInRange(s string) (ans []string) { for i := s[0]; i <= s[3]; i++ { for j := s[1]; j <= s[4]; j++ { ans = append(ans, string(i)+string(j)) } } - return ans + return } \ No newline at end of file diff --git a/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/Solution.ts b/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/Solution.ts new file mode 100644 index 0000000000000..181e7b5dbb747 --- /dev/null +++ b/solution/2100-2199/2194.Cells in a Range on an Excel Sheet/Solution.ts @@ -0,0 +1,9 @@ +function cellsInRange(s: string): string[] { + const ans: string[] = []; + for (let i = s.charCodeAt(0); i <= s.charCodeAt(3); ++i) { + for (let j = s.charCodeAt(1); j <= s.charCodeAt(4); ++j) { + ans.push(String.fromCharCode(i) + String.fromCharCode(j)); + } + } + return ans; +}