2043. 简易银行系统

This commit is contained in:
li-chx
2025-10-31 17:28:40 +08:00
parent 4f601269e8
commit c54a750759
2 changed files with 122 additions and 27 deletions
+59 -9
View File
@@ -13,13 +13,63 @@ pub fn make_arr(s: &str) -> Vec<i32> {
}
pub fn make_matrix(s: &str) -> Vec<Vec<i32>> {
s.trim_matches(&['[', ']'][..])
.split("],[")
.map(|row| {
row.trim_matches(&['[', ']'][..])
.split(',')
.map(|x| x.trim().parse::<i32>().unwrap())
.collect()
})
.collect()
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<i32>> = 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<i32> = row_str
.split(',')
.map(|x| {
let x = x.trim();
if x == "null" || x.is_empty() {
-1
} else {
x.parse::<i32>().unwrap()
}
})
.collect();
rows.push(row_vec);
}
rows
}