Compare commits
79
Commits
67d79e3553
...
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 | ||
|
|
1da8b6b735 | ||
|
|
0babcf1472 | ||
|
|
9648737245 | ||
|
|
93577c7ff2 | ||
|
|
4b8594e8e3 |
+129
@@ -1,3 +1,9 @@
|
||||
pub fn make_string_arr(s: &str) -> Vec<String> {
|
||||
s.trim_matches(&['[', ']'][..])
|
||||
.split(',')
|
||||
.map(|s| s.trim_matches(&['"',' ']).to_string())
|
||||
.collect()
|
||||
}
|
||||
pub fn make_arr(s: &str) -> Vec<i32> {
|
||||
s.trim_matches(&['[', ']'][..])
|
||||
.split(',')
|
||||
@@ -73,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
|
||||
}
|
||||
+19
-24
@@ -1,34 +1,29 @@
|
||||
mod arr;
|
||||
struct Solution;
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn intersection_size_two(intervals: Vec<Vec<i32>>) -> i32 {
|
||||
let mut tasks = intervals;
|
||||
tasks.sort_by_key(|task| task[1]);
|
||||
let mut stack:Vec<Vec<i32>> = Vec::new();
|
||||
stack.push(vec![-2,-2,0]);
|
||||
for task in tasks.iter() {
|
||||
let start = task[0];
|
||||
let end = task[1];
|
||||
let mut d = 2;
|
||||
let pos = stack.partition_point(|x| x[0] < start) - 1;
|
||||
d -= stack[stack.len() - 1][2] - stack[pos][2];
|
||||
if start <= stack[pos][1] {
|
||||
d -= stack[pos][1] - start + 1;
|
||||
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;
|
||||
}
|
||||
if d <= 0{
|
||||
continue;
|
||||
for i in 1..=word2.len() {
|
||||
dp[0][i] = dp[0][i - 1] + 1;
|
||||
}
|
||||
while end - stack[stack.len() - 1][1] <= d {
|
||||
let end = stack.pop().unwrap();
|
||||
d += end[1] - end[0] + 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);
|
||||
}
|
||||
stack.push(vec![end - d + 1, end,stack[stack.len() - 1][2] + d]);
|
||||
}
|
||||
stack[stack.len() - 1][2]
|
||||
dp[word1.len()][word2.len()]
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let result = Solution::intersection_size_two(arr::make_matrix("[[1,3],[3,7],[8,9]]"));
|
||||
println!("{:?}", result);
|
||||
println!(
|
||||
"{:?}",
|
||||
Solution::min_distance("intention".to_string(), "execution".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user