Skip to content

Commit 5e8720b

Browse files
committed
commit by crawler.py @netcan at 2019-03-16 00:33
1 parent 95a5742 commit 5e8720b

File tree

15 files changed

+468
-1
lines changed

15 files changed

+468
-1
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
本项目由`crawler.py`生成,代码自动爬取Leetcode-cn.com网站获取个人提交记录。使用方法:登陆Leetcode后记录cookie,设置环境变量`LEETCODE_COOKIE`,然后执行本脚本就能抓取指定语言的个人提交记录。
66

7-
目前已解决的题目(56 个,其中简单23 个,中等22 个, 困难11 个):
7+
目前已解决的题目(63 个,其中简单24 个,中等25 个, 困难14 个):
88
- [17. Letter Combinations of a Phone Number :star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0017.%20Letter%20Combinations%20of%20a%20Phone%20Number)
99
- [37. Sudoku Solver :star::star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0037.%20Sudoku%20Solver)
1010
- [50. Pow(x, n) :star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0050.%20Pow(x,%20n))
@@ -31,9 +31,11 @@
3131
- [223. Rectangle Area :star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0223.%20Rectangle%20Area)
3232
- [257. Binary Tree Paths :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0257.%20Binary%20Tree%20Paths)
3333
- [295. Find Median from Data Stream :star::star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0295.%20Find%20Median%20from%20Data%20Stream)
34+
- [312. Burst Balloons :star::star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0312.%20Burst%20Balloons)
3435
- [343. Integer Break :star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0343.%20Integer%20Break)
3536
- [349. Intersection of Two Arrays :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0349.%20Intersection%20of%20Two%20Arrays)
3637
- [363. Max Sum of Rectangle No Larger Than K :star::star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0363.%20Max%20Sum%20of%20Rectangle%20No%20Larger%20Than%20K)
38+
- [392. Is Subsequence :star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0392.%20Is%20Subsequence)
3739
- [405. Convert a Number to Hexadecimal :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0405.%20Convert%20a%20Number%20to%20Hexadecimal)
3840
- [437. Path Sum III :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0437.%20Path%20Sum%20III)
3941
- [443. String Compression :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0443.%20String%20Compression)
@@ -44,19 +46,24 @@
4446
- [563. Binary Tree Tilt :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0563.%20Binary%20Tree%20Tilt)
4547
- [630. Course Schedule III :star::star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0630.%20Course%20Schedule%20III)
4648
- [671. Second Minimum Node In a Binary Tree :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0671.%20Second%20Minimum%20Node%20In%20a%20Binary%20Tree)
49+
- [674. Longest Continuous Increasing Subsequence :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0674.%20Longest%20Continuous%20Increasing%20Subsequence)
4750
- [679. 24 Game :star::star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0679.%2024%20Game)
4851
- [687. Longest Univalue Path :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0687.%20Longest%20Univalue%20Path)
4952
- [695. Max Area of Island :star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0695.%20Max%20Area%20of%20Island)
5053
- [728. Self Dividing Numbers :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0728.%20Self%20Dividing%20Numbers)
5154
- [745. Find Smallest Letter Greater Than Target :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0745.%20Find%20Smallest%20Letter%20Greater%20Than%20Target)
55+
- [808. Number of Matching Subsequences :star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0808.%20Number%20of%20Matching%20Subsequences)
5256
- [866. Rectangle Overlap :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0866.%20Rectangle%20Overlap)
5357
- [891. Score After Flipping Matrix :star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0891.%20Score%20After%20Flipping%20Matrix)
5458
- [908. Middle of the Linked List :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0908.%20Middle%20of%20the%20Linked%20List)
5559
- [909. Stone Game :star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0909.%20Stone%20Game)
5660
- [958. Sort Array By Parity II :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0958.%20Sort%20Array%20By%20Parity%20II)
5761
- [972. Knight Dialer :star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n0972.%20Knight%20Dialer)
5862
- [1019. Squares of a Sorted Array :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n1019.%20Squares%20of%20a%20Sorted%20Array)
63+
- [1024. Triples with Bitwise AND Equal To Zero :star::star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n1024.%20Triples%20with%20Bitwise%20AND%20Equal%20To%20Zero)
64+
- [1033. Broken Calculator :star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n1033.%20Broken%20Calculator)
5965
- [1037. Minimum Number of K Consecutive Bit Flips :star::star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n1037.%20Minimum%20Number%20of%20K%20Consecutive%20Bit%20Flips)
66+
- [1042. Minimum Cost to Merge Stones :star::star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n1042.%20Minimum%20Cost%20to%20Merge%20Stones)
6067
- [1047. Maximize Sum Of Array After K Negations :star:](https://github.com/netcan/Leetcode-Rust/tree/master/n1047.%20Maximize%20Sum%20Of%20Array%20After%20K%20Negations)
6168
- [1048. Clumsy Factorial :star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n1048.%20Clumsy%20Factorial)
6269
- [1049. Minimum Domino Rotations For Equal Row :star::star:](https://github.com/netcan/Leetcode-Rust/tree/master/n1049.%20Minimum%20Domino%20Rotations%20For%20Equal%20Row)

n0312. Burst Balloons/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
### Burst Balloons :star::star::star:
3+
- 题目地址/Problem Url: [https://leetcode-cn.com/problems/burst-balloons](https://leetcode-cn.com/problems/burst-balloons)
4+
- 执行时间/Runtime: 4 ms
5+
- 内存消耗/Mem Usage: 2.6 MB
6+
- 通过日期/Accept Datetime: 2019-03-16 00:31
7+
8+
```rust
9+
// Author: Netcan @ https://github.com/netcan/Leetcode-Rust
10+
// Zhihu: https://www.zhihu.com/people/netcan
11+
12+
use std::iter;
13+
impl Solution {
14+
pub fn max_coins(mut nums: Vec<i32>) -> i32 {
15+
// dp[i][j]: 从i到j获得的最大硬币数量
16+
// coin[k]: 戳爆第k个气球得分
17+
// dp[i][j] = max(dp[i][j], dp[i][k - 1] + coin[i-1]*coin[k]*coin[j+1] + dp[k+1][j])
18+
let n = nums.len();
19+
if n == 0 { return 0; }
20+
else if n == 1 { return nums[0]; }
21+
22+
let mut coin = vec![1]; coin.append(&mut nums); coin.push(1);
23+
24+
let mut dp:Vec<Vec<i32>> =
25+
iter::repeat(vec![0; n + 1]).take(n + 1).collect();
26+
for i in 1..=n { dp[i][i] = coin[i-1] * coin[i] * coin[i+1]; }
27+
28+
for step in 1..n {
29+
for i in 1..=n-step {
30+
let j = i + step;
31+
for k in i..=j {
32+
let left = if k - 1 < i { 0 } else { dp[i][k - 1] };
33+
let right = if k + 1 > j { 0 } else { dp[k + 1][j] };
34+
dp[i][j] = dp[i][j].max(left + right + coin[i-1] * coin[k] * coin[j+1]);
35+
}
36+
}
37+
}
38+
dp[1][n]
39+
}
40+
}
41+
42+
```

n0312. Burst Balloons/main.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Author: Netcan @ https://github.com/netcan/Leetcode-Rust
2+
// Zhihu: https://www.zhihu.com/people/netcan
3+
4+
use std::iter;
5+
impl Solution {
6+
pub fn max_coins(mut nums: Vec<i32>) -> i32 {
7+
// dp[i][j]: 从i到j获得的最大硬币数量
8+
// coin[k]: 戳爆第k个气球得分
9+
// dp[i][j] = max(dp[i][j], dp[i][k - 1] + coin[i-1]*coin[k]*coin[j+1] + dp[k+1][j])
10+
let n = nums.len();
11+
if n == 0 { return 0; }
12+
else if n == 1 { return nums[0]; }
13+
14+
let mut coin = vec![1]; coin.append(&mut nums); coin.push(1);
15+
16+
let mut dp:Vec<Vec<i32>> =
17+
iter::repeat(vec![0; n + 1]).take(n + 1).collect();
18+
for i in 1..=n { dp[i][i] = coin[i-1] * coin[i] * coin[i+1]; }
19+
20+
for step in 1..n {
21+
for i in 1..=n-step {
22+
let j = i + step;
23+
for k in i..=j {
24+
let left = if k - 1 < i { 0 } else { dp[i][k - 1] };
25+
let right = if k + 1 > j { 0 } else { dp[k + 1][j] };
26+
dp[i][j] = dp[i][j].max(left + right + coin[i-1] * coin[k] * coin[j+1]);
27+
}
28+
}
29+
}
30+
dp[1][n]
31+
}
32+
}

n0392. Is Subsequence/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
### Is Subsequence :star::star:
3+
- 题目地址/Problem Url: [https://leetcode-cn.com/problems/is-subsequence](https://leetcode-cn.com/problems/is-subsequence)
4+
- 执行时间/Runtime: 4 ms
5+
- 内存消耗/Mem Usage: 4.6 MB
6+
- 通过日期/Accept Datetime: 2019-03-15 10:02
7+
8+
```rust
9+
// Author: Netcan @ https://github.com/netcan/Leetcode-Rust
10+
// Zhihu: https://www.zhihu.com/people/netcan
11+
12+
impl Solution {
13+
pub fn is_subsequence(s: String, t: String) -> bool {
14+
if s.len() == 0 { return true; }
15+
let (s, t) = (s.into_bytes(), t.into_bytes());
16+
let (mut i, mut j) = (0, 0);
17+
while i < s.len() && j < t.len() {
18+
if s[i] == t[j] {
19+
i += 1;
20+
if i == s.len() { return true; }
21+
}
22+
j += 1;
23+
}
24+
false
25+
}
26+
}
27+
28+
29+
```

n0392. Is Subsequence/main.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Author: Netcan @ https://github.com/netcan/Leetcode-Rust
2+
// Zhihu: https://www.zhihu.com/people/netcan
3+
4+
impl Solution {
5+
pub fn is_subsequence(s: String, t: String) -> bool {
6+
if s.len() == 0 { return true; }
7+
let (s, t) = (s.into_bytes(), t.into_bytes());
8+
let (mut i, mut j) = (0, 0);
9+
while i < s.len() && j < t.len() {
10+
if s[i] == t[j] {
11+
i += 1;
12+
if i == s.len() { return true; }
13+
}
14+
j += 1;
15+
}
16+
false
17+
}
18+
}
19+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
### Longest Continuous Increasing Subsequence :star:
3+
- 题目地址/Problem Url: [https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence](https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence)
4+
- 执行时间/Runtime: 0 ms
5+
- 内存消耗/Mem Usage: 2.7 MB
6+
- 通过日期/Accept Datetime: 2019-03-15 12:09
7+
8+
```rust
9+
// Author: Netcan @ https://github.com/netcan/Leetcode-Rust
10+
// Zhihu: https://www.zhihu.com/people/netcan
11+
12+
use std::cmp::max;
13+
impl Solution {
14+
pub fn find_length_of_lcis(nums: Vec<i32>) -> i32 {
15+
if nums.len() == 0 { return 0; }
16+
let (mut length, mut max_length) = (1, 1);
17+
nums.iter().skip(1).fold(nums[0], |a, b| {
18+
if a < *b { length += 1; max_length = max(max_length, length) }
19+
else { length = 1; }
20+
*b
21+
});
22+
max_length
23+
}
24+
}
25+
26+
27+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Author: Netcan @ https://github.com/netcan/Leetcode-Rust
2+
// Zhihu: https://www.zhihu.com/people/netcan
3+
4+
use std::cmp::max;
5+
impl Solution {
6+
pub fn find_length_of_lcis(nums: Vec<i32>) -> i32 {
7+
if nums.len() == 0 { return 0; }
8+
let (mut length, mut max_length) = (1, 1);
9+
nums.iter().skip(1).fold(nums[0], |a, b| {
10+
if a < *b { length += 1; max_length = max(max_length, length) }
11+
else { length = 1; }
12+
*b
13+
});
14+
max_length
15+
}
16+
}
17+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
### Number of Matching Subsequences :star::star:
3+
- 题目地址/Problem Url: [https://leetcode-cn.com/problems/number-of-matching-subsequences](https://leetcode-cn.com/problems/number-of-matching-subsequences)
4+
- 执行时间/Runtime: 164 ms
5+
- 内存消耗/Mem Usage: 4.7 MB
6+
- 通过日期/Accept Datetime: 2019-03-15 11:17
7+
8+
```rust
9+
// Author: Netcan @ https://github.com/netcan/Leetcode-Rust
10+
// Zhihu: https://www.zhihu.com/people/netcan
11+
12+
use std::collections::HashMap;
13+
impl Solution {
14+
pub fn num_matching_subseq(s: String, words: Vec<String>) -> i32 {
15+
let s = s.into_bytes();
16+
let words: Vec<Vec<u8>> = words.into_iter().map(|w| { w.into_bytes() }).collect();
17+
18+
// 预处理s
19+
let mut s_char_pos = HashMap::new();
20+
for (pos, c) in s.iter().enumerate() {
21+
let c_pos_list = s_char_pos.entry(c).or_insert(Vec::new());
22+
c_pos_list.push(pos);
23+
}
24+
25+
let mut cnt = 0;
26+
for word in &words {
27+
let mut w_pos = 0;
28+
let mut is_seq = true;
29+
for w in word {
30+
if !s_char_pos.contains_key(&w) {
31+
is_seq = false; break;
32+
}
33+
let pos_list = s_char_pos.get(&w).unwrap();
34+
match pos_list.binary_search(&w_pos) {
35+
Ok(idx) => { w_pos = pos_list[idx] + 1; },
36+
Err(idx) => {
37+
if(idx >= 0 && idx < pos_list.len()) { w_pos = pos_list[idx] + 1; }
38+
else { is_seq = false; break; }
39+
}
40+
}
41+
}
42+
if is_seq { cnt += 1; }
43+
}
44+
45+
cnt
46+
}
47+
}
48+
49+
50+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Author: Netcan @ https://github.com/netcan/Leetcode-Rust
2+
// Zhihu: https://www.zhihu.com/people/netcan
3+
4+
use std::collections::HashMap;
5+
impl Solution {
6+
pub fn num_matching_subseq(s: String, words: Vec<String>) -> i32 {
7+
let s = s.into_bytes();
8+
let words: Vec<Vec<u8>> = words.into_iter().map(|w| { w.into_bytes() }).collect();
9+
10+
// 预处理s
11+
let mut s_char_pos = HashMap::new();
12+
for (pos, c) in s.iter().enumerate() {
13+
let c_pos_list = s_char_pos.entry(c).or_insert(Vec::new());
14+
c_pos_list.push(pos);
15+
}
16+
17+
let mut cnt = 0;
18+
for word in &words {
19+
let mut w_pos = 0;
20+
let mut is_seq = true;
21+
for w in word {
22+
if !s_char_pos.contains_key(&w) {
23+
is_seq = false; break;
24+
}
25+
let pos_list = s_char_pos.get(&w).unwrap();
26+
match pos_list.binary_search(&w_pos) {
27+
Ok(idx) => { w_pos = pos_list[idx] + 1; },
28+
Err(idx) => {
29+
if(idx >= 0 && idx < pos_list.len()) { w_pos = pos_list[idx] + 1; }
30+
else { is_seq = false; break; }
31+
}
32+
}
33+
}
34+
if is_seq { cnt += 1; }
35+
}
36+
37+
cnt
38+
}
39+
}
40+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
### Triples with Bitwise AND Equal To Zero :star::star::star:
3+
- 题目地址/Problem Url: [https://leetcode-cn.com/problems/triples-with-bitwise-and-equal-to-zero](https://leetcode-cn.com/problems/triples-with-bitwise-and-equal-to-zero)
4+
- 执行时间/Runtime: 144 ms
5+
- 内存消耗/Mem Usage: 3 MB
6+
- 通过日期/Accept Datetime: 2019-03-15 17:52
7+
8+
```rust
9+
// Author: Netcan @ https://github.com/netcan/Leetcode-Rust
10+
// Zhihu: https://www.zhihu.com/people/netcan
11+
12+
impl Solution {
13+
pub fn count_triplets(nums: Vec<i32>) -> i32 {
14+
let max_num = nums.iter().skip(1).fold(nums[0], |a, b| { a.max(*b) } ) as usize;
15+
// dp[n] 为A[i] & A[j] = n的个数
16+
let mut dp = vec![0; max_num + 1];
17+
for ai in &nums {
18+
for aj in &nums { dp[(ai & aj) as usize] += 1; }
19+
}
20+
21+
let mut ans = 0;
22+
for a in &nums {
23+
for b in (0..=max_num as i32) {
24+
if a & b == 0 {
25+
ans += dp[b as usize];
26+
}
27+
}
28+
}
29+
30+
ans
31+
}
32+
}
33+
34+
35+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Author: Netcan @ https://github.com/netcan/Leetcode-Rust
2+
// Zhihu: https://www.zhihu.com/people/netcan
3+
4+
impl Solution {
5+
pub fn count_triplets(nums: Vec<i32>) -> i32 {
6+
let max_num = nums.iter().skip(1).fold(nums[0], |a, b| { a.max(*b) } ) as usize;
7+
// dp[n] 为A[i] & A[j] = n的个数
8+
let mut dp = vec![0; max_num + 1];
9+
for ai in &nums {
10+
for aj in &nums { dp[(ai & aj) as usize] += 1; }
11+
}
12+
13+
let mut ans = 0;
14+
for a in &nums {
15+
for b in (0..=max_num as i32) {
16+
if a & b == 0 {
17+
ans += dp[b as usize];
18+
}
19+
}
20+
}
21+
22+
ans
23+
}
24+
}
25+

0 commit comments

Comments
 (0)