Skip to content

Commit 4356820

Browse files
author
Zhang Xiaodong
committed
solve 38
1 parent eb3c23f commit 4356820

File tree

3 files changed

+110
-6
lines changed

3 files changed

+110
-6
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ mod s0034_find_first_and_last_position_of_element_in_sorted_array;
3333
mod s0035_search_insert_position;
3434
mod s0036_valid_sudoku;
3535
mod s0037_sudoku_solver;
36+
mod s0038_count_and_say;

src/solution/s0037_sudoku_solver.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,15 @@ impl Solution {
9494
break;
9595
}
9696
if !found {
97-
(i, sc) = stack.pop().unwrap();
98-
let (m, n) = (i/9, i%9);
97+
let (mut ni, mut nc) = stack.pop().unwrap();
98+
let (m, n) = (ni/9, ni%9);
9999
let blk_idx = m / 3 * 3 + n / 3;
100100
board[m][n] = '.';
101-
rows[m].remove(&sc);
102-
cols[n].remove(&sc);
103-
blks[blk_idx].remove(&sc);
104-
sc = (sc as u8 + 1) as char;
101+
rows[m].remove(&nc);
102+
cols[n].remove(&nc);
103+
blks[blk_idx].remove(&nc);
104+
sc = (nc as u8 + 1) as char;
105+
i = ni;
105106
continue 'outer;
106107
}
107108
},

src/solution/s0038_count_and_say.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* [38] 外观数列
3+
*
4+
* 给定一个正整数 n ,输出外观数列的第 n 项。
5+
* 「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。
6+
* 你可以将其视作是由递归公式定义的数字字符串序列:
7+
*
8+
* countAndSay(1) = "1"
9+
* countAndSay(n) 是对 countAndSay(n-1) 的描述,然后转换成另一个数字字符串。
10+
*
11+
* 前五项如下:
12+
*
13+
* 1. 1
14+
* 2. 11
15+
* 3. 21
16+
* 4. 1211
17+
* 5. 111221
18+
* 第一项是数字 1
19+
* 描述前一项,这个数是 1 即 “ 一 个 1 ”,记作 "11"
20+
* 描述前一项,这个数是 11 即 “ 二 个 1 ” ,记作 "21"
21+
* 描述前一项,这个数是 21 即 “ 一 个 2 + 一 个 1 ” ,记作 "1211"
22+
* 描述前一项,这个数是 1211 即 “ 一 个 1 + 一 个 2 + 二 个 1 ” ,记作 "111221"
23+
*
24+
* 要 描述 一个数字字符串,首先要将字符串分割为 最小 数量的组,每个组都由连续的最多 相同字符 组成。然后对于每个组,先描述字符的数量,然后描述字符,形成一个描述组。要将描述转换为数字字符串,先将每组中的字符数量用数字替换,再将所有描述组连接起来。
25+
* 例如,数字字符串 "3322251" 的描述如下图:
26+
* <img alt="" src="https://pic.leetcode-cn.com/1629874763-TGmKUh-image.png" style="width: 581px; height: 172px;" />
27+
*
28+
*
29+
* 示例 1:
30+
*
31+
* 输入:n = 1
32+
* 输出:"1"
33+
* 解释:这是一个基本样例。
34+
*
35+
* 示例 2:
36+
*
37+
* 输入:n = 4
38+
* 输出:"1211"
39+
* 解释:
40+
* countAndSay(1) = "1"
41+
* countAndSay(2) = 读 "1" = 一 个 1 = "11"
42+
* countAndSay(3) = 读 "11" = 二 个 1 = "21"
43+
* countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"
44+
*
45+
*
46+
* 提示:
47+
*
48+
* 1 <= n <= 30
49+
*
50+
*/
51+
pub struct Solution {}
52+
53+
// problem: https://leetcode.cn/problems/count-and-say/
54+
// discuss: https://leetcode.cn/problems/count-and-say/discuss/?currentPage=1&orderBy=most_votes&query=
55+
56+
// submission codes start here
57+
58+
impl Solution {
59+
pub fn count_and_say(n: i32) -> String {
60+
let mut res: String = "".to_string();
61+
for i in 0..n {
62+
if i == 0 {
63+
res = "1".to_string();
64+
} else {
65+
let mut t = '.';
66+
let mut cnt = 0;
67+
let mut s = "".to_string();
68+
for c in res.chars() {
69+
if t == '.' {
70+
cnt = 1;
71+
t = c;
72+
} else if t != c {
73+
s = format!("{}{}{}", s, cnt, t);
74+
cnt = 1;
75+
t = c;
76+
} else {
77+
cnt += 1;
78+
}
79+
}
80+
res = format!("{}{}{}", s, cnt, t);
81+
}
82+
}
83+
res
84+
}
85+
}
86+
87+
// submission codes end
88+
89+
#[cfg(test)]
90+
mod tests {
91+
use super::*;
92+
93+
#[test]
94+
fn test_38() {
95+
assert_eq!(Solution::count_and_say(1), "1");
96+
assert_eq!(Solution::count_and_say(2), "11");
97+
assert_eq!(Solution::count_and_say(3), "21");
98+
assert_eq!(Solution::count_and_say(4), "1211");
99+
assert_eq!(Solution::count_and_say(5), "111221");
100+
assert_eq!(Solution::count_and_say(6), "312211");
101+
}
102+
}

0 commit comments

Comments
 (0)