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

来源:互联网 发布:ubuntu 远程唤醒 编辑:程序博客网 时间:2024/06/05 23:41
<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);}}



                                             
0 0
原创粉丝点击