diff --git a/solution/2000-2099/2075.Decode the Slanted Ciphertext/README.md b/solution/2000-2099/2075.Decode the Slanted Ciphertext/README.md index 356f12671104f..9bf2436861a9f 100644 --- a/solution/2000-2099/2075.Decode the Slanted Ciphertext/README.md +++ b/solution/2000-2099/2075.Decode the Slanted Ciphertext/README.md @@ -79,7 +79,13 @@ ## 解法 -### 方法一 +### 方法一:模拟 + +我们先计算出矩阵的列数 $cols = \text{len}(encodedText) / rows$,然后按照题目描述的规则,从左上角开始遍历矩阵,将字符添加到答案中。 + +最后返回答案,注意去掉末尾的空格。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $encodedText$ 的长度。 @@ -120,22 +126,42 @@ public: string decodeCiphertext(string encodedText, int rows) { string ans; int cols = encodedText.size() / rows; - for (int j = 0; j < cols; ++j) - for (int x = 0, y = j; x < rows && y < cols; ++x, ++y) + for (int j = 0; j < cols; ++j) { + for (int x = 0, y = j; x < rows && y < cols; ++x, ++y) { ans += encodedText[x * cols + y]; - while (ans.back() == ' ') ans.pop_back(); + } + } + while (ans.size() && ans.back() == ' ') { + ans.pop_back(); + } return ans; } }; ``` +```go +func decodeCiphertext(encodedText string, rows int) string { + ans := []byte{} + cols := len(encodedText) / rows + for j := 0; j < cols; j++ { + for x, y := 0, j; x < rows && y < cols; x, y = x+1, y+1 { + ans = append(ans, encodedText[x*cols+y]) + } + } + for len(ans) > 0 && ans[len(ans)-1] == ' ' { + ans = ans[:len(ans)-1] + } + return string(ans) +} +``` + ```ts function decodeCiphertext(encodedText: string, rows: number): string { const cols = Math.ceil(encodedText.length / rows); - let ans = []; + const ans: string[] = []; for (let k = 0; k <= cols; k++) { for (let i = 0, j = k; i < rows && j < cols; i++, j++) { - ans.push(encodedText.charAt(i * cols + j)); + ans.push(encodedText[i * cols + j]); } } return ans.join('').trimEnd(); diff --git a/solution/2000-2099/2075.Decode the Slanted Ciphertext/README_EN.md b/solution/2000-2099/2075.Decode the Slanted Ciphertext/README_EN.md index 7851abd66775c..e25f8f7b110f5 100644 --- a/solution/2000-2099/2075.Decode the Slanted Ciphertext/README_EN.md +++ b/solution/2000-2099/2075.Decode the Slanted Ciphertext/README_EN.md @@ -63,7 +63,13 @@ The blue arrows show how we can find originalText from encodedText. ## Solutions -### Solution 1 +### Solution 1: Simulation + +First, we calculate the number of columns in the matrix $cols = \text{len}(encodedText) / rows$. Then, following the rules described in the problem, we start traversing the matrix from the top left corner, adding characters to the answer. + +Finally, we return the answer, making sure to remove any trailing spaces. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $encodedText$. @@ -104,22 +110,42 @@ public: string decodeCiphertext(string encodedText, int rows) { string ans; int cols = encodedText.size() / rows; - for (int j = 0; j < cols; ++j) - for (int x = 0, y = j; x < rows && y < cols; ++x, ++y) + for (int j = 0; j < cols; ++j) { + for (int x = 0, y = j; x < rows && y < cols; ++x, ++y) { ans += encodedText[x * cols + y]; - while (ans.back() == ' ') ans.pop_back(); + } + } + while (ans.size() && ans.back() == ' ') { + ans.pop_back(); + } return ans; } }; ``` +```go +func decodeCiphertext(encodedText string, rows int) string { + ans := []byte{} + cols := len(encodedText) / rows + for j := 0; j < cols; j++ { + for x, y := 0, j; x < rows && y < cols; x, y = x+1, y+1 { + ans = append(ans, encodedText[x*cols+y]) + } + } + for len(ans) > 0 && ans[len(ans)-1] == ' ' { + ans = ans[:len(ans)-1] + } + return string(ans) +} +``` + ```ts function decodeCiphertext(encodedText: string, rows: number): string { const cols = Math.ceil(encodedText.length / rows); - let ans = []; + const ans: string[] = []; for (let k = 0; k <= cols; k++) { for (let i = 0, j = k; i < rows && j < cols; i++, j++) { - ans.push(encodedText.charAt(i * cols + j)); + ans.push(encodedText[i * cols + j]); } } return ans.join('').trimEnd(); diff --git a/solution/2000-2099/2075.Decode the Slanted Ciphertext/Solution.cpp b/solution/2000-2099/2075.Decode the Slanted Ciphertext/Solution.cpp index 34374747fad99..49a9c4dc1c8ef 100644 --- a/solution/2000-2099/2075.Decode the Slanted Ciphertext/Solution.cpp +++ b/solution/2000-2099/2075.Decode the Slanted Ciphertext/Solution.cpp @@ -3,10 +3,14 @@ class Solution { string decodeCiphertext(string encodedText, int rows) { string ans; int cols = encodedText.size() / rows; - for (int j = 0; j < cols; ++j) - for (int x = 0, y = j; x < rows && y < cols; ++x, ++y) + for (int j = 0; j < cols; ++j) { + for (int x = 0, y = j; x < rows && y < cols; ++x, ++y) { ans += encodedText[x * cols + y]; - while (ans.back() == ' ') ans.pop_back(); + } + } + while (ans.size() && ans.back() == ' ') { + ans.pop_back(); + } return ans; } }; \ No newline at end of file diff --git a/solution/2000-2099/2075.Decode the Slanted Ciphertext/Solution.go b/solution/2000-2099/2075.Decode the Slanted Ciphertext/Solution.go new file mode 100644 index 0000000000000..b798f1a564d88 --- /dev/null +++ b/solution/2000-2099/2075.Decode the Slanted Ciphertext/Solution.go @@ -0,0 +1,13 @@ +func decodeCiphertext(encodedText string, rows int) string { + ans := []byte{} + cols := len(encodedText) / rows + for j := 0; j < cols; j++ { + for x, y := 0, j; x < rows && y < cols; x, y = x+1, y+1 { + ans = append(ans, encodedText[x*cols+y]) + } + } + for len(ans) > 0 && ans[len(ans)-1] == ' ' { + ans = ans[:len(ans)-1] + } + return string(ans) +} \ No newline at end of file diff --git a/solution/2000-2099/2075.Decode the Slanted Ciphertext/Solution.ts b/solution/2000-2099/2075.Decode the Slanted Ciphertext/Solution.ts index b0acd772e36d6..e991036728a2a 100644 --- a/solution/2000-2099/2075.Decode the Slanted Ciphertext/Solution.ts +++ b/solution/2000-2099/2075.Decode the Slanted Ciphertext/Solution.ts @@ -1,9 +1,9 @@ function decodeCiphertext(encodedText: string, rows: number): string { const cols = Math.ceil(encodedText.length / rows); - let ans = []; + const ans: string[] = []; for (let k = 0; k <= cols; k++) { for (let i = 0, j = k; i < rows && j < cols; i++, j++) { - ans.push(encodedText.charAt(i * cols + j)); + ans.push(encodedText[i * cols + j]); } } return ans.join('').trimEnd();