leetcode简单题27 N.119 杨辉三角II rust描述

07-19 1308阅读

leetcode简单题27 N.119 杨辉三角II rust描述

 

// 直接生成杨辉三角当前行
pub fn get_row(row_index: i32) -> Vec {
    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 {
    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 {
    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() {}
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]