美文网首页
119. Pascal's Triangle II

119. Pascal's Triangle II

作者: Kim_9527 | 来源:发表于2017-04-14 18:08 被阅读6次

    Given an index k, return the kth row of the Pascal's triangle.
    For example, given k = 3,
    Return [1,3,3,1].

    Already Pass Solution

        public IList<int> GetRow(int rowIndex) {
            //(1)
            int[] temp = new int[rowIndex+1];
            temp[0] = 1;
            for(int i = 1;i< rowIndex+1; i++)
            {
                for(int j = rowIndex; j > 0; j--)
                {
                    temp[j] = temp[j] + temp[j - 1] ;
                }
            }
            IList<int> result = new List<int>();
            foreach(int t in temp)
            {
                result.Add(t);
            }
            return result;
        }
    

    思路:
    1.逆序冲最后一个值开始求起,从而降低空间复杂度
    2.利用帕斯卡三角形的形成原理计算每一行的值

    待解决:
    (1)直接使用一种数据结构存储答案是否可行

    相关文章

      网友评论

          本文标题:119. Pascal's Triangle II

          本文链接:https://www.haomeiwen.com/subject/uchgattx.html