mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 844
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package leetcode
|
||||
|
||||
func backspaceCompare(S string, T string) bool {
|
||||
s := make([]rune, 0)
|
||||
for _, c := range S {
|
||||
if c == '#' {
|
||||
if len(s) > 0 {
|
||||
s = s[:len(s)-1]
|
||||
}
|
||||
} else {
|
||||
s = append(s, c)
|
||||
}
|
||||
}
|
||||
s2 := make([]rune, 0)
|
||||
for _, c := range T {
|
||||
if c == '#' {
|
||||
if len(s2) > 0 {
|
||||
s2 = s2[:len(s2)-1]
|
||||
}
|
||||
} else {
|
||||
s2 = append(s2, c)
|
||||
}
|
||||
}
|
||||
return string(s) == string(s2)
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question844 struct {
|
||||
para844
|
||||
ans844
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para844 struct {
|
||||
s string
|
||||
t string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans844 struct {
|
||||
one bool
|
||||
}
|
||||
|
||||
func Test_Problem844(t *testing.T) {
|
||||
|
||||
qs := []question844{
|
||||
|
||||
question844{
|
||||
para844{"ab#c", "ad#c"},
|
||||
ans844{true},
|
||||
},
|
||||
|
||||
question844{
|
||||
para844{"ab##", "c#d#"},
|
||||
ans844{true},
|
||||
},
|
||||
|
||||
question844{
|
||||
para844{"a##c", "#a#c"},
|
||||
ans844{true},
|
||||
},
|
||||
|
||||
question844{
|
||||
para844{"a#c", "b"},
|
||||
ans844{false},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 844------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans844, q.para844
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, backspaceCompare(p.s, p.t))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
68
Algorithms/844. Backspace String Compare/README.md
Normal file
68
Algorithms/844. Backspace String Compare/README.md
Normal file
@@ -0,0 +1,68 @@
|
||||
# [844. Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
```c
|
||||
Input: S = "ab#c", T = "ad#c"
|
||||
Output: true
|
||||
Explanation: Both S and T become "ac".
|
||||
```
|
||||
|
||||
Example 2:
|
||||
|
||||
```c
|
||||
Input: S = "ab##", T = "c#d#"
|
||||
Output: true
|
||||
Explanation: Both S and T become "".
|
||||
```
|
||||
|
||||
Example 3:
|
||||
|
||||
```c
|
||||
Input: S = "a##c", T = "#a#c"
|
||||
Output: true
|
||||
Explanation: Both S and T become "c".
|
||||
```
|
||||
|
||||
Example 4:
|
||||
|
||||
```c
|
||||
Input: S = "a#c", T = "b"
|
||||
Output: false
|
||||
Explanation: S becomes "c" while T becomes "b".
|
||||
```
|
||||
|
||||
|
||||
Note:
|
||||
|
||||
- 1 <= S.length <= 200
|
||||
- 1 <= T.length <= 200
|
||||
- S and T only contain lowercase letters and '#' characters.
|
||||
|
||||
|
||||
Follow up:
|
||||
|
||||
- Can you solve it in O(N) time and O(1) space?
|
||||
|
||||
## 题目大意
|
||||
|
||||
|
||||
给 2 个字符串,如果遇到 # 号字符,就回退一个字符。问最终的 2 个字符串是否完全一致。
|
||||
|
||||
这一题可以用栈的思想来模拟,遇到 # 字符就回退一个字符。不是 # 号就入栈一个字符。比较最终 2 个字符串即可。
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user