Skip to content

Commit 7a4055a

Browse files
authored
feat: add solutions to lc problem: No.2075 (#2610)
No.2075.Decode the Slanted Ciphertext
1 parent ff2ee08 commit 7a4055a

File tree

5 files changed

+86
-17
lines changed

5 files changed

+86
-17
lines changed

solution/2000-2099/2075.Decode the Slanted Ciphertext/README.md

+32-6
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@
7979

8080
## 解法
8181

82-
### 方法一
82+
### 方法一:模拟
83+
84+
我们先计算出矩阵的列数 $cols = \text{len}(encodedText) / rows$,然后按照题目描述的规则,从左上角开始遍历矩阵,将字符添加到答案中。
85+
86+
最后返回答案,注意去掉末尾的空格。
87+
88+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $encodedText$ 的长度。
8389

8490
<!-- tabs:start -->
8591

@@ -120,22 +126,42 @@ public:
120126
string decodeCiphertext(string encodedText, int rows) {
121127
string ans;
122128
int cols = encodedText.size() / rows;
123-
for (int j = 0; j < cols; ++j)
124-
for (int x = 0, y = j; x < rows && y < cols; ++x, ++y)
129+
for (int j = 0; j < cols; ++j) {
130+
for (int x = 0, y = j; x < rows && y < cols; ++x, ++y) {
125131
ans += encodedText[x * cols + y];
126-
while (ans.back() == ' ') ans.pop_back();
132+
}
133+
}
134+
while (ans.size() && ans.back() == ' ') {
135+
ans.pop_back();
136+
}
127137
return ans;
128138
}
129139
};
130140
```
131141
142+
```go
143+
func decodeCiphertext(encodedText string, rows int) string {
144+
ans := []byte{}
145+
cols := len(encodedText) / rows
146+
for j := 0; j < cols; j++ {
147+
for x, y := 0, j; x < rows && y < cols; x, y = x+1, y+1 {
148+
ans = append(ans, encodedText[x*cols+y])
149+
}
150+
}
151+
for len(ans) > 0 && ans[len(ans)-1] == ' ' {
152+
ans = ans[:len(ans)-1]
153+
}
154+
return string(ans)
155+
}
156+
```
157+
132158
```ts
133159
function decodeCiphertext(encodedText: string, rows: number): string {
134160
const cols = Math.ceil(encodedText.length / rows);
135-
let ans = [];
161+
const ans: string[] = [];
136162
for (let k = 0; k <= cols; k++) {
137163
for (let i = 0, j = k; i < rows && j < cols; i++, j++) {
138-
ans.push(encodedText.charAt(i * cols + j));
164+
ans.push(encodedText[i * cols + j]);
139165
}
140166
}
141167
return ans.join('').trimEnd();

solution/2000-2099/2075.Decode the Slanted Ciphertext/README_EN.md

+32-6
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ The blue arrows show how we can find originalText from encodedText.
6363

6464
## Solutions
6565

66-
### Solution 1
66+
### Solution 1: Simulation
67+
68+
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.
69+
70+
Finally, we return the answer, making sure to remove any trailing spaces.
71+
72+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $encodedText$.
6773

6874
<!-- tabs:start -->
6975

@@ -104,22 +110,42 @@ public:
104110
string decodeCiphertext(string encodedText, int rows) {
105111
string ans;
106112
int cols = encodedText.size() / rows;
107-
for (int j = 0; j < cols; ++j)
108-
for (int x = 0, y = j; x < rows && y < cols; ++x, ++y)
113+
for (int j = 0; j < cols; ++j) {
114+
for (int x = 0, y = j; x < rows && y < cols; ++x, ++y) {
109115
ans += encodedText[x * cols + y];
110-
while (ans.back() == ' ') ans.pop_back();
116+
}
117+
}
118+
while (ans.size() && ans.back() == ' ') {
119+
ans.pop_back();
120+
}
111121
return ans;
112122
}
113123
};
114124
```
115125
126+
```go
127+
func decodeCiphertext(encodedText string, rows int) string {
128+
ans := []byte{}
129+
cols := len(encodedText) / rows
130+
for j := 0; j < cols; j++ {
131+
for x, y := 0, j; x < rows && y < cols; x, y = x+1, y+1 {
132+
ans = append(ans, encodedText[x*cols+y])
133+
}
134+
}
135+
for len(ans) > 0 && ans[len(ans)-1] == ' ' {
136+
ans = ans[:len(ans)-1]
137+
}
138+
return string(ans)
139+
}
140+
```
141+
116142
```ts
117143
function decodeCiphertext(encodedText: string, rows: number): string {
118144
const cols = Math.ceil(encodedText.length / rows);
119-
let ans = [];
145+
const ans: string[] = [];
120146
for (let k = 0; k <= cols; k++) {
121147
for (let i = 0, j = k; i < rows && j < cols; i++, j++) {
122-
ans.push(encodedText.charAt(i * cols + j));
148+
ans.push(encodedText[i * cols + j]);
123149
}
124150
}
125151
return ans.join('').trimEnd();

solution/2000-2099/2075.Decode the Slanted Ciphertext/Solution.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ class Solution {
33
string decodeCiphertext(string encodedText, int rows) {
44
string ans;
55
int cols = encodedText.size() / rows;
6-
for (int j = 0; j < cols; ++j)
7-
for (int x = 0, y = j; x < rows && y < cols; ++x, ++y)
6+
for (int j = 0; j < cols; ++j) {
7+
for (int x = 0, y = j; x < rows && y < cols; ++x, ++y) {
88
ans += encodedText[x * cols + y];
9-
while (ans.back() == ' ') ans.pop_back();
9+
}
10+
}
11+
while (ans.size() && ans.back() == ' ') {
12+
ans.pop_back();
13+
}
1014
return ans;
1115
}
1216
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func decodeCiphertext(encodedText string, rows int) string {
2+
ans := []byte{}
3+
cols := len(encodedText) / rows
4+
for j := 0; j < cols; j++ {
5+
for x, y := 0, j; x < rows && y < cols; x, y = x+1, y+1 {
6+
ans = append(ans, encodedText[x*cols+y])
7+
}
8+
}
9+
for len(ans) > 0 && ans[len(ans)-1] == ' ' {
10+
ans = ans[:len(ans)-1]
11+
}
12+
return string(ans)
13+
}

solution/2000-2099/2075.Decode the Slanted Ciphertext/Solution.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
function decodeCiphertext(encodedText: string, rows: number): string {
22
const cols = Math.ceil(encodedText.length / rows);
3-
let ans = [];
3+
const ans: string[] = [];
44
for (let k = 0; k <= cols; k++) {
55
for (let i = 0, j = k; i < rows && j < cols; i++, j++) {
6-
ans.push(encodedText.charAt(i * cols + j));
6+
ans.push(encodedText[i * cols + j]);
77
}
88
}
99
return ans.join('').trimEnd();

0 commit comments

Comments
 (0)