阅读量:2
// 直接生成杨辉三角当前行 pub fn get_row(row_index: i32) -> Vec<i32> { let mut row = vec![1; (row_index + 1) as usize]; for i in 1..=row_index as usize { for j in (1..i).rev() { row[j] = row[j] + row[j - 1]; } } row } // 空间优化的方法 pub fn get_row2(row_index: i32) -> Vec<i32> { let mut row = vec![1; (row_index + 1) as usize]; for i in 1..=row_index as usize { for j in (1..i).rev() { row[j] = row[j] + row[j - 1]; } } row } // 数学组合数公式 pub fn get_row3(row_index: i32) -> Vec<i32> { let mut row = vec![1; (row_index + 1) as usize]; for k in 1..=row_index as usize { row[k] = (row[k - 1] as u64 * (row_index - k as i32 + 1) as u64 / k as u64) as i32; } row } fn main() {}