Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2075 #2610

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add solutions to lc problem: No.2075
No.2075.Decode the Slanted Ciphertext
  • Loading branch information
yanglbme committed Apr 18, 2024
commit 4c2726f257fce37f3c4b8ffde74cd994ba009f5e
38 changes: 32 additions & 6 deletions solution/2000-2099/2075.Decode the Slanted Ciphertext/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@

## 解法

### 方法一
### 方法一:模拟

我们先计算出矩阵的列数 $cols = \text{len}(encodedText) / rows$,然后按照题目描述的规则,从左上角开始遍历矩阵,将字符添加到答案中。

最后返回答案,注意去掉末尾的空格。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $encodedText$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -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();
Expand Down
38 changes: 32 additions & 6 deletions solution/2000-2099/2075.Decode the Slanted Ciphertext/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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$.

<!-- tabs:start -->

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
13 changes: 13 additions & 0 deletions solution/2000-2099/2075.Decode the Slanted Ciphertext/Solution.go
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
Loading