Compare commits
74
Commits
1da8b6b735
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
551db6decf | ||
|
|
c1fe8fd658 | ||
|
|
23a4622f11 | ||
|
|
ebd3f9445c | ||
|
|
50fba25511 | ||
|
|
ece3af5149 | ||
|
|
a19c7573a7 | ||
|
|
fe0577dd06 | ||
|
|
fcf3f21a52 | ||
|
|
cb73a780cf | ||
|
|
86ec2ee252 | ||
|
|
fc8857537e | ||
|
|
8cc7ce864c | ||
|
|
b88bfb137b
|
||
|
|
985c978958
|
||
|
|
25645920ff
|
||
|
|
44ce8b9fd4
|
||
|
|
afa91fe380
|
||
|
|
6e59cabd84
|
||
|
|
00bc762b5e
|
||
|
|
9452c4e658 | ||
|
|
a9da2f1227
|
||
|
|
fa937a142f
|
||
|
|
6213d83856
|
||
|
|
d0da066c58
|
||
|
|
4460a2f028
|
||
|
|
4ee2acd245
|
||
|
|
6074be16ce
|
||
|
|
11a7d6775d
|
||
|
|
93d4b712d5
|
||
|
|
a467de459b
|
||
|
|
653e68c8f1
|
||
|
|
671347dfde
|
||
|
|
023fe9ec14
|
||
|
|
a6ca63686a
|
||
|
|
283b4c1518
|
||
|
|
444863b22d
|
||
|
|
8cac824082
|
||
|
|
8cc2ab780c
|
||
|
|
98eafcac0e
|
||
|
|
ecb53e3f3a
|
||
|
|
cca3dc4a88
|
||
|
|
8a381984c9 | ||
|
|
d7f5b98838 | ||
|
|
f1f47604f8 | ||
|
|
60447f6bf8 | ||
|
|
b0641080b5 | ||
|
|
bb6a7dc773 | ||
|
|
bb640829ee | ||
|
|
6c8fa06ce1 | ||
|
|
143ff15f87 | ||
|
|
d5eafc86f4 | ||
|
|
f91a4b2fe6 | ||
|
|
3d72b848d2 | ||
|
|
bde9d0f8b2 | ||
|
|
e615d4f611 | ||
|
|
f80a39e514 | ||
|
|
bd8207767a | ||
|
|
c18ad8c1b0 | ||
|
|
566c6a8414 | ||
|
|
f8fd02dcb8 | ||
|
|
563c0b3589 | ||
|
|
1772ba6c99 | ||
|
|
495075eef2 | ||
|
|
0f894b29a8 | ||
|
|
54bea679f0 | ||
|
|
873bf741e6 | ||
|
|
7efe4d98bb | ||
|
|
0f8400d0c9 | ||
|
|
9a82ec82f7 | ||
|
|
376065acb6 | ||
|
|
cd8b2b31e3 | ||
|
|
08f6ce4581 | ||
|
|
702b016368 |
+123
@@ -79,3 +79,126 @@ pub fn make_matrix(s: &str) -> Vec<Vec<i32>> {
|
||||
|
||||
rows
|
||||
}
|
||||
|
||||
pub fn make_char_matrix(s: &str) -> Vec<Vec<char>> {
|
||||
let s = s.trim();
|
||||
|
||||
// Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]"
|
||||
let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') {
|
||||
&s[1..s.len() - 1]
|
||||
} else {
|
||||
s
|
||||
}
|
||||
.trim();
|
||||
|
||||
if inner.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let bytes = inner.as_bytes();
|
||||
let mut i = 0usize;
|
||||
let mut rows: Vec<Vec<char>> = Vec::new();
|
||||
while i < bytes.len() {
|
||||
// find next '['
|
||||
while i < bytes.len() && bytes[i] != b'[' {
|
||||
i += 1;
|
||||
}
|
||||
if i >= bytes.len() {
|
||||
break;
|
||||
}
|
||||
let start = i + 1; // byte index after '['
|
||||
i = start;
|
||||
// find matching ']'
|
||||
while i < bytes.len() && bytes[i] != b']' {
|
||||
i += 1;
|
||||
}
|
||||
let end = i; // exclusive
|
||||
// move i past ']' for next iteration (if in bounds)
|
||||
if i < bytes.len() {
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if end <= start {
|
||||
// empty row like []
|
||||
rows.push(Vec::new());
|
||||
continue;
|
||||
}
|
||||
|
||||
let row_str = &inner[start..end];
|
||||
let row_vec: Vec<char> = row_str
|
||||
.split(',')
|
||||
.map(|x| {
|
||||
let x = x.trim().trim_matches('"');
|
||||
if x == "null" || x.is_empty() {
|
||||
' '
|
||||
} else {
|
||||
x.to_string().chars().nth(0).unwrap()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
rows.push(row_vec);
|
||||
}
|
||||
|
||||
rows
|
||||
}
|
||||
pub fn make_string_matrix(s: &str) -> Vec<Vec<String>> {
|
||||
let s = s.trim();
|
||||
|
||||
// Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]"
|
||||
let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') {
|
||||
&s[1..s.len() - 1]
|
||||
} else {
|
||||
s
|
||||
}
|
||||
.trim();
|
||||
|
||||
if inner.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let bytes = inner.as_bytes();
|
||||
let mut i = 0usize;
|
||||
let mut rows: Vec<Vec<String>> = Vec::new();
|
||||
while i < bytes.len() {
|
||||
// find next '['
|
||||
while i < bytes.len() && bytes[i] != b'[' {
|
||||
i += 1;
|
||||
}
|
||||
if i >= bytes.len() {
|
||||
break;
|
||||
}
|
||||
let start = i + 1; // byte index after '['
|
||||
i = start;
|
||||
// find matching ']'
|
||||
while i < bytes.len() && bytes[i] != b']' {
|
||||
i += 1;
|
||||
}
|
||||
let end = i; // exclusive
|
||||
// move i past ']' for next iteration (if in bounds)
|
||||
if i < bytes.len() {
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if end <= start {
|
||||
// empty row like []
|
||||
rows.push(Vec::new());
|
||||
continue;
|
||||
}
|
||||
|
||||
let row_str = &inner[start..end];
|
||||
let row_vec: Vec<String> = row_str
|
||||
.split(',')
|
||||
.map(|x| {
|
||||
let x = x.trim().trim_matches('"');
|
||||
if x == "null" || x.is_empty() {
|
||||
"".to_string()
|
||||
} else {
|
||||
x.to_string()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
rows.push(row_vec);
|
||||
}
|
||||
|
||||
rows
|
||||
}
|
||||
+21
-22
@@ -1,30 +1,29 @@
|
||||
use crate::arr::make_matrix;
|
||||
|
||||
struct Solution;
|
||||
mod arr;
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn number_of_paths(grid: Vec<Vec<i32>>, k: i32) -> i32 {
|
||||
let mut arr = vec![vec![vec![0; k as usize]; grid[0].len()]; grid.len()];
|
||||
let r#mod = 1e9 as i32 + 7;
|
||||
arr[0][0][(grid[0][0] % k) as usize] = 1;
|
||||
for i in 0..grid.len() {
|
||||
for j in 0..grid[i].len() {
|
||||
if i < grid.len() - 1 {
|
||||
for l in 0..k {
|
||||
arr[i + 1][j][((l + grid[i + 1][j]) % k) as usize] = (arr[i + 1][j][((l + grid[i + 1][j]) % k) as usize] + arr[i][j][l as usize]) % r#mod;
|
||||
}
|
||||
}
|
||||
if j < grid[0].len() - 1 {
|
||||
for l in 0..k {
|
||||
arr[i][j + 1][((l + grid[i][j + 1]) % k) as usize] = (arr[i][j + 1][((l + grid[i][j + 1]) % k) as usize] + arr[i][j][l as usize]) % r#mod;
|
||||
}
|
||||
}
|
||||
pub fn min_distance(word1: String, word2: String) -> i32 {
|
||||
let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1];
|
||||
let word1 = word1.chars().collect::<Vec<char>>();
|
||||
let word2 = word2.chars().collect::<Vec<char>>();
|
||||
for i in 1..=word1.len() {
|
||||
dp[i][0] = dp[i - 1][0] + 1;
|
||||
}
|
||||
for i in 1..=word2.len() {
|
||||
dp[0][i] = dp[0][i - 1] + 1;
|
||||
}
|
||||
for i in 0..word1.len() {
|
||||
for j in 0..word2.len() {
|
||||
dp[i + 1][j + 1] = (dp[i][j] + if word1[i] != word2[j] { 1 } else { 0 })
|
||||
.min(dp[i][j + 1] + 1)
|
||||
.min(dp[i + 1][j] + 1);
|
||||
}
|
||||
}
|
||||
arr[grid.len() - 1][grid[0].len() - 1][0]
|
||||
dp[word1.len()][word2.len()]
|
||||
}
|
||||
}
|
||||
fn main() {
|
||||
let result = Solution::number_of_paths(make_matrix("[[5,2,4],[3,0,5],[0,7,2]]"), 3);
|
||||
println!("{:?}", result);
|
||||
println!(
|
||||
"{:?}",
|
||||
Solution::min_distance("intention".to_string(), "execution".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user