Skip to content

Commit 6108eb2

Browse files
committed
update 0005 ~ 0007
1 parent 48f079b commit 6108eb2

File tree

9 files changed

+180
-351
lines changed

9 files changed

+180
-351
lines changed

Algorithms/0005.longest_palindromic_substring/README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ Output: "bb"
1717
## 解题思路
1818
题目要求寻找字符串中的最长回文。
1919
当然,我们可以使用下面的程序判断字符串s[i:j+1]是否是回文。
20-
```go
21-
func isPalindromic(s *string, i, j int ) bool {
22-
for i< j {
23-
if (*s)[i] != (*s)[j] {
24-
return false
25-
}
26-
i++
27-
j--
20+
```rust
21+
let expand_around_center = |left: i32, right: i32| -> (usize, usize) {
22+
let mut l = left;
23+
let mut r = right;
24+
25+
while l >= 0 && r < n as i32 && s_chars[l as usize] == s_chars[r as usize] {
26+
l -= 1;
27+
r += 1;
2828
}
29-
return true
30-
}
29+
30+
((l + 1) as usize, (r - l - 1) as usize)
31+
};
3132
```
3233
但是,这样就没有利用回文的一下特点,假定回文的长度为l,x为任意字符
3334
1. 当l为奇数时,回文的`正中间段`只会是,“x”,或“xxx”,或“xxxxx”,或...
Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,37 @@
1-
package problem0005
2-
3-
func longestPalindrome(s string) string {
4-
if len(s) < 2 { // 肯定是回文,直接返回
5-
return s
6-
}
7-
8-
// 最长回文的首字符索引,和最长回文的长度
9-
begin, maxLen := 0, 1
10-
11-
// 在 for 循环中
12-
// b 代表回文的**首**字符索引号,
13-
// e 代表回文的**尾**字符索引号,
14-
// i 代表回文`正中间段`首字符的索引号
15-
// 在每一次for循环中
16-
// 先从i开始,利用`正中间段`所有字符相同的特性,让b,e分别指向`正中间段`的首尾
17-
// 再从`正中间段`向两边扩张,让b,e分别指向此`正中间段`为中心的最长回文的首尾
18-
for i := 0; i < len(s); { // 以s[i]为`正中间段`首字符开始寻找最长回文。
19-
if len(s)-i <= maxLen/2 {
20-
// 因为i是回文`正中间段`首字符的索引号
21-
// 假设此时能找到的最长回文的长度为l, 则,l <= (len(s)-i)*2 - 1
22-
// 如果,len(s)-i <= maxLen/2 成立
23-
// 则,l <= maxLen - 1
24-
// 则,l < maxLen
25-
// 所以,无需再找下去了。
26-
break
27-
}
28-
29-
b, e := i, i
30-
for e < len(s)-1 && s[e+1] == s[e] {
31-
e++
32-
// 循环结束后,s[b:e+1]是一串相同的字符串
33-
}
34-
35-
// 下一个回文的`正中间段`的首字符只会是s[e+1]
36-
// 为下一次循环做准备
37-
i = e + 1
38-
39-
for e < len(s)-1 && b > 0 && s[e+1] == s[b-1] {
40-
e++
41-
b--
42-
// 循环结束后,s[b:e+1]是这次能找到的最长回文。
43-
}
44-
45-
newLen := e + 1 - b
46-
// 创新记录的话,就更新记录
47-
if newLen > maxLen {
48-
begin = b
49-
maxLen = newLen
50-
}
51-
}
52-
53-
return s[begin : begin+maxLen]
54-
}
1+
impl Solution {
2+
pub fn longest_palindrome(s: String) -> String {
3+
4+
let n = s.len();
5+
if n < 2 {
6+
return s;
7+
}
8+
9+
let chars: Vec<char> = s.chars().collect();
10+
let mut start = 0;
11+
let mut max_len = 1;
12+
13+
let expand_around_center = |left: i32, right: i32| -> (usize, usize) {
14+
let mut l = left;
15+
let mut r = right;
16+
17+
while l >= 0 && r < n as i32 && s_chars[l as usize] == s_chars[r as usize] {
18+
l -= 1;
19+
r += 1;
20+
}
21+
22+
((l + 1) as usize, (r - l - 1) as usize)
23+
};
24+
25+
for i in 0..n {
26+
let (start1, len1) = expand_around_center(i as i32, i as i32);
27+
let (start2, len2) = expand_around_center(i as i32, (i + 1) as i32);
28+
29+
if len1 > max_len {
30+
start = start1;
31+
max_len = len1;
32+
}
33+
}
34+
35+
s[start..start + max_len].to_string()
36+
}
37+
}
Lines changed: 44 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,47 @@
1-
package problem0005
2-
3-
import (
4-
"testing"
5-
6-
"github.com/stretchr/testify/assert"
7-
)
8-
9-
type para struct {
10-
one string
11-
}
12-
13-
type ans struct {
14-
one string
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn longest_palindrome(s: String) -> String {
5+
if s.is_empty() {
6+
return String::new();
7+
}
8+
9+
let chars: Vec<char> = s.chars().collect();
10+
let n = chars.len();
11+
let mut start = 0;
12+
let mut max_len = 1;
13+
14+
// 动态规划数组,dp[i][j] 表示 s[i..=j] 是否为回文串
15+
let mut dp = vec![vec![false; n]; n];
16+
17+
// 所有长度为 1 的子串都是回文串
18+
for i in 0..n {
19+
dp[i][i] = true;
20+
}
21+
22+
// 检查长度为 2 及以上的子串
23+
for len in 2..=n {
24+
for i in 0..n - len + 1 {
25+
let j = i + len - 1;
26+
if len == 2 {
27+
dp[i][j] = chars[i] == chars[j];
28+
} else {
29+
dp[i][j] = dp[i + 1][j - 1] && chars[i] == chars[j];
30+
}
31+
32+
if dp[i][j] && len > max_len {
33+
start = i;
34+
max_len = len;
35+
}
36+
}
37+
}
38+
39+
chars[start..start + max_len].iter().collect()
40+
}
1541
}
1642

17-
type question struct {
18-
p para
19-
a ans
20-
}
21-
22-
func Test_OK(t *testing.T) {
23-
ast := assert.New(t)
24-
25-
qs := []question{
26-
question{
27-
p: para{
28-
one: "babad",
29-
},
30-
a: ans{
31-
one: "bab",
32-
},
33-
},
34-
question{
35-
p: para{
36-
one: "cbbd",
37-
},
38-
a: ans{
39-
one: "bb",
40-
},
41-
},
42-
question{
43-
p: para{
44-
one: "abbcccddcccbba",
45-
},
46-
a: ans{
47-
one: "abbcccddcccbba",
48-
},
49-
},
50-
question{
51-
p: para{
52-
one: "a",
53-
},
54-
a: ans{
55-
one: "a",
56-
},
57-
},
58-
}
59-
60-
for _, q := range qs {
61-
a, p := q.a, q.p
62-
ast.Equal(a.one, longestPalindrome(p.one), "输入:%v", p)
63-
}
43+
fn main() {
44+
let result = Solution::longest_palindrome("babad".to_string());
45+
let result2 = Solution::longest_palindrome("abbcccddcccbba".to_string());
46+
println!("{}, {}", result, result2);
6447
}

Algorithms/0006.zigzag_conversion/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ Y I R
1313
And then read line by line: "PAHNAPLSIIGYIR"
1414
Write the code that will take a string and make this conversion given a number of rows:
1515

16-
```go
17-
func convert(text string, nRows int) string
18-
```
19-
2016
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
2117

2218
## 解题思路
Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,23 @@
1-
package problem0006
2-
3-
import (
4-
"bytes"
5-
)
6-
7-
func convert(s string, numRows int) string {
8-
if numRows == 1 || len(s) <= numRows {
9-
return s
10-
}
11-
12-
res := bytes.Buffer{}
13-
// p pace 步距
14-
p := numRows*2 - 2
15-
16-
// 处理第一行
17-
for i := 0; i < len(s); i += p {
18-
res.WriteByte(s[i])
19-
}
20-
21-
// 处理中间的行
22-
for r := 1; r <= numRows-2; r++ {
23-
// 添加r行的第一个字符
24-
res.WriteByte(s[r])
25-
26-
for k := p; k-r < len(s); k += p {
27-
res.WriteByte(s[k-r])
28-
if k+r < len(s) {
29-
res.WriteByte(s[k+r])
30-
}
31-
}
32-
}
33-
34-
// 处理最后一行
35-
for i := numRows - 1; i < len(s); i += p {
36-
res.WriteByte(s[i])
37-
}
38-
39-
return res.String()
40-
}
1+
impl Solution {
2+
pub fn convert(s: String, num_rows: i32) -> String {
3+
let num_rows = num_rows as usize;
4+
if num_rows <= 1 || s.len() <= num_rows {
5+
return s;
6+
}
7+
8+
let mut rows: Vec<String> = vec![String::new(); num_rows];
9+
let mut row_index = 0;
10+
let mut direction = 1;
11+
12+
for c in s.chars() {
13+
rows[row_index].push(c);
14+
row_index = (row_index as i32 + direction) as usize;
15+
16+
if row_index == 0 || row_index == num_rows - 1 {
17+
direction *= -1;
18+
}
19+
}
20+
21+
rows.concat()
22+
}
23+
}

0 commit comments

Comments
 (0)