3212. 统计 X 和 Y 频数相等的子矩阵数量
This commit is contained in:
+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
|
||||
}
|
||||
Reference in New Issue
Block a user