首页 > 其他分享 >Leecode 杨辉三角Ⅱ

Leecode 杨辉三角Ⅱ

时间:2024-03-21 16:45:14浏览次数:12  
标签:int res List getRow rowIndex Leecode 杨辉三角

Day 7 第二题

我的思路和上一篇的杨辉三角一致,只不过将List获取层数的代码List.get(0).add()修改成数组
class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> getRow = new ArrayList<Integer>();
        
        int[][] dp = new int[rowIndex+1][rowIndex+1];
        getRow.add(1);
        dp[0][0] = 1;

        for(int i = 1;i<=rowIndex;i++){
            dp[i][0] = 1; 
            
            for(int j=1;j<=i-1;j++){
                dp[i][j] = dp[i-1][j-1]+dp[i-1][j];
                if(i==rowIndex){
                    getRow.add(dp[i][j]);
                }
            }   
            dp[i][i] = 1;    
            if(i==rowIndex){
                getRow.add(1);
            }           
        }
        
        
       return getRow;
    }
}
lincs设计 节约内存空间 的代码
class Solution {
    public List<Integer> getRow(int N) {
        List<Integer> res = new ArrayList<>(N + 1);
        for (int i = 0; i <= N; i++) {
            res.add(1);
            for (int j = i - 1; j > 0; j--) {
                res.set(j, res.get(j) + res.get(j - 1));
            }
        }
        return res;
    }
}

标签:int,res,List,getRow,rowIndex,Leecode,杨辉三角
From: https://www.cnblogs.com/xytang-mini-juan/p/18087702

相关文章

  • Leecode 二叉树的中序遍历
    Day6第三题这是一道让我崩溃的题,因为一个笔误root.right写成了root.left改了好久。classSolution{publicList<Integer>inorderTraversal(TreeNoderoot){List<Integer>listRoot=newArrayList<Integer>();if(root!=null){listRo......
  • Leecode 盛最多的水
    Day6刷题我的思路:利用两层循环,暴力搜索所有组合的面积,找出最大值。importjava.util.*;classSolution{publicintmaxArea(int[]height){//找height和间距intmaxArea=0,iArea;for(intp1=0;p1<height.length-1;p1++){......
  • Leecode 移动零
    Day6刷题我的解题思路:利用双指针,一个指针不断向前移动,表征是否交换完成,另一个指针负责当次交换的位置。classSolution{publicvoidmoveZeroes(int[]nums){//当指针p1指向的元素为0时,将其挪到后面OUT:for(intp1=0;p1<nums.......
  • 118. 杨辉三角c
    /***Returnanarrayofarraysofsize*returnSize.*Thesizesofthearraysarereturnedas*returnColumnSizesarray.*Note:Bothreturnedarrayand*columnSizesarraymustbemalloced,assumecallercallsfree().*/int**generate(intnumRows,in......
  • Leecode 二叉树的前序遍历
    Day2刷题我的思路:用数组list存储遍历结果,利用ArrayList的方法实现嵌套!importjava.util.*;classSolution{//defininganarraylistpublicList<Integer>preorderTraversal(TreeNoderoot){List<Integer>Traversal=newArrayList<>();......
  • Leecode 搜索插入位置
    Day2刷题我的思路:利用二分法解决问题,不过由于情况没有好好分类,以及循环判定条件不合适,会出现超出运行时间的bug。classSolution{publicintsearchInsert(int[]nums,inttarget){intlength=nums.length;intStartIdx=0;intFinal......
  • Leecode 最长公共前缀
    Day1刷题此题没有写出来,仅附上力扣官方代码:classSolution{publicStringlongestCommonPrefix(String[]strs){if(strs==null||strs.length==0){return"";}Stringprefix=strs[0];intcount=strs.length;......
  • Leecode 将罗马数字转换为整型
    Day1刷题我的解题思路利用罗马数字与整型的转换规律,利用哈希表来生成键值对。classSolution{publicintromanToInt(Strings){intsum=0;HashMap<Character,Integer>RomanInt=newHashMap<Character,Integer>();RomanInt.put('I......
  • 数据结构ArrayList之杨辉三角庖丁解牛!
    题外话先给大家露一手我对杨辉三角的理解,虽说标题是庖丁解牛,但是还是虚心请教一下大家,有什么意见都可以提出!正题思维逻辑先画个杨辉三角,有几点需要大家注意一下1.杨辉三角其实在代码里就是一个二维数组,图中i代表行但是是从0开始的,而j则代表每行的元素2.如果想......
  • Leecode 求两数之和
    Day1刷题我的解题思路是按照第一个元素往后排,不重复的找相加的组合,判断是否为target值,时间复杂度较高,为\(\mathcal{O}(n^2)\)。classSolution{publicint[]twoSum(int[]nums,inttarget){intflag=1;while(flag==1){for(in......