锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

【leetcode】P119杨辉三角II

时间:2022-08-22 21:30:00 p119举升传感器

///给出一个非负索引 k,其中 k ≤ 33.返回杨辉三角 k 行。  // //  // // 在杨辉三角中,每个数字都是左上角和右上角的数字和。  // // 示例:  // // 输入: 3 //输出: [1,3,3,1] //  // // 进阶:  // // 你可以优化你的算法 O(k) 空间复杂吗?  // Related Topics 数组  // ?? 195 ?? 0  package leetcode.editor.cn;  import java.util.ArrayList; import java.util.List;  //Java:杨辉三角 II public class P119PascalsTriangleIi { 
            public static void main(String[] args) { 
                Solution solution = new P119PascalsTriangleIi().new Solution();         // TO TEST         List<Integer> row = solution.getRow(3);         System.out.println(row);     }      //leetcode submit region begin(Prohibit modification and deletion)     class Solution { 
                public List<Integer> getRow(int rowIndex) { 
                    ArrayList<Integer> res = new ArrayList<>(rowIndex   1);             if (rowIndex< 0)
                return res;
            res.add(1);
            for (int i = 1; i <= rowIndex; i++) { 
       
                for (int j = i - 1; j > 0; j--) { 
       
                    res.set(j,res.get(j)+res.get(j-1));
                }
                res.add(1);
            }
            return res;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}


锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章