Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

lib.rs 982B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #[cfg(test)]
  2. mod tests {
  3. #[test]
  4. fn it_works() {
  5. assert_eq!(2 + 2, 4);
  6. }
  7. }
  8. pub struct PascalsTriangle {
  9. height: u32,
  10. }
  11. impl PascalsTriangle {
  12. pub fn new(i: u32) -> Self {
  13. PascalsTriangle { height: i }
  14. }
  15. pub fn rows(&self) -> Vec<Vec<u32>> {
  16. let rows = self.height as usize;
  17. let mut matrix = Vec::with_capacity(rows);
  18. for line in 0..rows {
  19. let current = line as usize;
  20. matrix.push(Vec::with_capacity(current + 1));
  21. matrix[current].push(1);
  22. if current > 1 {
  23. let previous = current - 1;
  24. for index in 1..current {
  25. let add = matrix[previous][index - 1] + matrix[previous][index];
  26. matrix[current].push(add);
  27. }
  28. }
  29. if current > 0 {
  30. matrix[current].push(1);
  31. }
  32. }
  33. println!("{:?}", matrix);
  34. matrix
  35. }
  36. }