都说Rust性能好,但是也得代码写得好,猜猜下面两个代码哪个快
. - 力扣(LeetCode)
use std::collections::HashMap;
use lazy_static::lazy_static;
lazy_static! {
static ref DIGIT: HashMap<char, usize> = {
let mut m = HashMap::new();
for c in '1'..':' {
m.insert(c, Solution::to_digit(c) as usize - 1);
}
m
};
}
impl Solution {
pub fn to_digit(c: char) -> u32 {
match c.to_digit(10) {
None => 0,
Some(i) => i,
}
}
pub const SUB_INDEX: [[usize; 9]; 9] = [
[0, 0, 0, 1, 1, 1, 2, 2, 2],
[0, 0, 0, 1, 1, 1, 2, 2, 2],
[0, 0, 0, 1, 1, 1, 2, 2, 2],
[3, 3, 3, 4, 4, 4, 5, 5, 5],
[3, 3, 3, 4, 4, 4, 5, 5, 5],
[3, 3, 3, 4, 4, 4, 5, 5, 5],
[6, 6, 6, 7, 7, 7, 8, 8, 8],
[6, 6, 6, 7, 7, 7, 8, 8, 8],
[6, 6, 6, 7, 7, 7, 8, 8, 8],
];
pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
let mut line_count: [[i32; 9]; 9] = [[0; 9]; 9];
let mut colume_count: [[i32; 9]; 9] = [[0; 9]; 9];
let mut sub_count: [[i32; 9]; 9] = [[0; 9]; 9];
let mut i = 0;
let mut j = 0;
for line in board {
for colume in line {
if colume != '.' {
let digit = match DIGIT.get(&colume) {
Some(i) => *i,
None => return false
};
if line_count[i][digit] == 0 {
line_count[i][digit] = 1;
} else {
return false;
}
if colume_count[j][digit] == 0 {
colume_count[j][digit] = 1;
} else {
return false;
}
if sub_count[Solution::SUB_INDEX[i][j]][digit] == 0 {
sub_count[Solution::SUB_INDEX[i][j]][digit] = 1;
} else {
return false;
}
}
j += 1;
}
j = 0;
i += 1;
}
true
}
}
use std::collections::HashSet;
impl Solution {
pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
let mut row:HashSet<char> = HashSet::with_capacity(9);
let mut blocks:Vec<HashSet<char>> = vec![HashSet::with_capacity(9);3];
let mut col:Vec<HashSet<char>> = vec![HashSet::with_capacity(9);9];
for (idx,i) in board.iter().enumerate(){
if idx % 3 == 0{
for i in 0..3{
blocks[i].clear();
}
}
for (jdx,&j) in i.iter().enumerate(){
if j == '.'{
continue;
}
let temp = jdx/3;
if row.contains(&j) || col[jdx].contains(&j) || blocks[temp].contains(&j) {
//println!("{},{}",idx,jdx);
return false;
}
row.insert(j);
col[jdx].insert(j);
blocks[temp].insert(j);
}
row.clear();
}
true
}
}
Perf 分析 Perf+火焰图进行性能瓶颈分析-CSDN博客