118. Pascal's Triangle 简单数组问题

原创 2016年05月30日 16:53:52
<div class="question-title" style="box-sizing: border-box; padding-bottom: 10px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(238, 238, 238); margin-top: 1em; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><h3 style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin-top: 0px; margin-bottom: 10px; font-size: 24px; display: inline-block; margin-right: 0.5em;">118. Pascal's Triangle</h3> <span class="fa fa-lg fa-star add-favor not-favor" data-toggle="popover" data-trigger="hover" data-placement="top" data-content="save to favorite" ng-click="detailCtrl.favor()" ng-class="{'favor': detailCtrl.is_favor, 'not-favor': !detailCtrl.is_favor}" data-original-title="" title="" style="box-sizing: border-box; display: inline-block; font-stretch: normal; line-height: 0.75em; font-family: FontAwesome; font-size: 1.33333em; text-rendering: auto; -webkit-font-smoothing: antialiased; vertical-align: -15%; color: grey; cursor: pointer; position: relative; top: -3px;"></span><a target=_blank class="pull-right btn btn-default" href="https://leetcode.com/problems/pascals-triangle/submissions/" style="box-sizing: border-box; color: rgb(51, 51, 51); text-decoration: none; display: inline-block; padding: 6px 12px; margin-bottom: 0px; line-height: 1.42857; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; -webkit-user-select: none; border: 1px solid rgb(204, 204, 204); border-radius: 4px; float: right; background-image: none; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0px 0px; background-repeat: initial;">My Submissions</a><div class="pull-right btn-group right-pad" style="box-sizing: border-box; position: relative; display: inline-block; vertical-align: middle; float: right; margin-right: 15px;"><button type="button" class="btn btn-default active" style="margin: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 14px; line-height: 1.42857; font-family: inherit; color: rgb(51, 51, 51); overflow: visible; cursor: pointer; padding: 6px 12px; white-space: nowrap; vertical-align: middle; -webkit-user-select: none; border: 1px solid rgb(173, 173, 173); border-radius: 4px 0px 0px 4px; outline: 0px; box-shadow: rgba(0, 0, 0, 0.121569) 0px 3px 5px inset; position: relative; float: left; z-index: 2; background-image: none; background-color: rgb(230, 230, 230);">Question</button><button type="button" class="btn btn-default " data-toggle="tooltip" data-placement="top" title="" data-original-title="Not available yet, please check back later." style="margin: 0px 0px 0px -1px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 14px; line-height: 1.42857; font-family: inherit; color: rgb(51, 51, 51); overflow: visible; cursor: pointer; padding: 6px 12px; white-space: nowrap; vertical-align: middle; -webkit-user-select: none; border: 1px solid rgb(204, 204, 204); border-radius: 0px 4px 4px 0px; position: relative; float: left; background-image: none; background-color: rgb(255, 255, 255);">Editorial Solution</button></div></div><div class="row col-md-12" style="box-sizing: border-box; margin-right: -15px; margin-left: -15px; position: relative; min-height: 1px; padding-right: 15px; padding-left: 15px; float: left; width: 1140px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><span class="total-ac text-info" style="box-sizing: border-box; color: rgb(49, 112, 143); font-size: 13px; vertical-align: text-bottom;">Total Accepted: <span style="box-sizing: border-box; font-weight: 700;">85485</span></span> <span class="total-submit text-info" style="box-sizing: border-box; color: rgb(49, 112, 143); margin-left: 15px; font-size: 13px; vertical-align: text-bottom;">Total Submissions: <span style="box-sizing: border-box; font-weight: 700;">253159</span></span> <span class="total-submit text-info" style="box-sizing: border-box; color: rgb(49, 112, 143); margin-left: 15px; font-size: 13px; vertical-align: text-bottom;">Difficulty: <span style="box-sizing: border-box; font-weight: 700;">Easy</span></span></div><div class="row" style="box-sizing: border-box; margin-right: -15px; margin-left: -15px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><div class="col-md-12" style="box-sizing: border-box; position: relative; min-height: 1px; padding-right: 15px; padding-left: 15px; float: left; width: 1170px;"><div class="question-content" style="box-sizing: border-box; margin-left: 20px; margin-top: 20px; margin-bottom: 5px; line-height: 30px; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(221, 221, 221); padding-left: 20px; padding-bottom: 2px;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">Given <span style="box-sizing: border-box;">numRows</span>, generate the first <span style="box-sizing: border-box;">numRows</span> of Pascal's triangle.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">For example, given <span style="box-sizing: border-box;">numRows</span> = 5,<br style="box-sizing: border-box;" />Return</p><pre style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 9.5px; margin-top: 0px; margin-bottom: 10px; line-height: 1.42857; word-break: break-all; word-wrap: break-word; border: 1px solid rgb(204, 204, 204); border-radius: 4px; background-color: rgb(245, 245, 245);">[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]
package bupt;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class PascalsTriangle118 {
	
public static List<List<Integer>> generate(int numRows) {
	
        List<List<Integer>> result=new LinkedList();//定义返回类型的数组
        if(numRows==0) return result;
        Integer[] start=new Integer[]{1};//定义Integer类型的数组
        List<Integer> temp=Arrays.asList(start);
        result.add(temp);
        for(int i=1;i<=numRows;i++)
        {
        	if(i==1) continue;
        	Integer[] nums=new Integer[i];
        	Object[] prior=result.get(i-2).toArray();//将List转为Object数组
        	for(int j=0;j<nums.length;j++)
        	{
        		
        		if(j==0||j==nums.length-1)
        		{
        			
        			nums[j]=1;
        			
        			
        		}
        		else
        		{
        			nums[j]=(Integer)prior[j]+(Integer)prior[j-1];//核心部分,转化为加上上一个数组的相邻两个元素
        		}
        		
        		
        	}
        result.add(Arrays.asList(nums));
        }
        return result;
    }

	public static void main(String[] args) {
		
		List li=PascalsTriangle118.generate(0);
		System.out.println(li);

	}

}




                    

(数组)leetcode 118:Pascal's Triangle

刷题笔记 leetcode 118:Pascal's Triangle (tag:数组)

LeetCode - 118. Pascal's Triangle

这道题目是有关三角形的题目,因此可能会涉及到二维数组的问题,在处理这样的三角形问题的时候,有一个比较好的方法就是将三角形当作半个矩阵来看,这样可以比较方便地理清元素下标之间的关系。比如题目中给出的例子...

LeetCode 118. Pascal's Triangle

题目很简单,输入一个数字n,返回一个n行的杨辉三角。解题思路也很简单,我们知道杨辉三角的第n+1行第m+1列的元素为c(n,m)=n!/[m!(n-m)!]。所以我们写个一个函数可以返回c(n,m),...

118. Pascal's Triangle [easy] (Python)

题目链接https://leetcode.com/problems/pascals-triangle/题目原文 Given numRows, generate the first numRows ...

LeetCode 118 Pascal's Triangle(帕斯卡三角形)(vector)

翻译给定一个行数字,生成它的帕斯卡三角形。例如,给定numRows = 5, 返回: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4...
  • NoMasp
  • NoMasp
  • 2016年01月23日 09:28
  • 2382

118、119 pascal's triangle I、II---Array-LeetCode

118 pascal's triangle I---Array-LeetCode 找规律 a = b + c。 119 pascal's triangle II---Array-LeetCode ...

LeetCode-118、119. Pascal's Triangle (JAVA)杨辉三角

LeetCode-118、119. Pascal's Triangle (JAVA)杨辉三角,118. Pascal's Triangle ,119. Pascal's Triangle II...

【leetcode】118. Pascal's Triangle

一、题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given nu...

<LeetCode OJ> 118 / 119 Pascal's Triangle(I / II)

118. Pascal's Triangle My Submissions Question Total Accepted: 73965 Total Submissions: 227957 D...

Leetcode_118_Pascal's Triangle

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41827325 Given numRows, genera...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:118. Pascal's Triangle 简单数组问题
举报原因:
原因补充:

(最多只允许输入30个字)