【LeetCode從零單排】No118 Pascal's Triangle
題目
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
程式碼
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result=new ArrayList<List<Integer>>();
// List<Integer> temp=new List<Integer>();
if(numRows==0) return result;
if(numRows==1) {
List<Integer> temp=new ArrayList<Integer>();
temp.add(1);
result.add(temp);
return result;
}
if(numRows==2){
List<Integer> temp1=new ArrayList<Integer>();
List<Integer> temp2=new ArrayList<Integer>();
temp1.add(1);
temp2.add(1);
temp2.add(1);
result.add(temp1);
result.add(temp2);
return result;
}
List<Integer> temp1=new ArrayList<Integer>();
List<Integer> temp2=new ArrayList<Integer>();
temp1.add(1);
temp2.add(1);
temp2.add(1);
result.add(temp1);
result.add(temp2);
for(int i=3;i<=numRows;i++){
List<Integer> temp=new ArrayList<Integer>();
temp.add(1);
for(int j=0;j<i-2;j++){
temp.add(result.get(i-2).get(j)+result.get(i-2).get(j+1));
}
temp.add(1);
result.add(temp);
}
return result;
}
}
/********************************
* 本文來自部落格 “李博Garvin“
* 轉載請標明出處:http://blog.csdn.net/buptgshengod
******************************************/
相關文章
- Leetcode Pascal's TriangleLeetCode
- leetcode Pascal's Triangle IILeetCode
- Leetcode-Pascal's TriangleLeetCode
- Leetcode-Pascal's Triangle IILeetCode
- Pascal's Triangle II Leetcode javaLeetCodeJava
- Pascal's Triangle leetcode java(楊輝三角)LeetCodeJava
- 【LeetCode從零單排】No38.CountAndSayLeetCode
- 【LeetCode從零單排】No.7 Reverse IntegerLeetCode
- 【LeetCode從零單排】No20.ValidParenthesesLeetCode
- 【LeetCode從零單排】No19.RemoveNthNodeFromEndofListLeetCodeREM
- 【LeetCode從零單排】No21.MergeTwoSortedListsLeetCode
- 【LeetCode從零單排】No27.Remove ElementLeetCodeREM
- 【LeetCode從零單排】No28 Implement strStr()LeetCode
- 【LeetCode從零單排】No22.Generate ParenthesesLeetCode
- 每天一道LeetCode--118. Pascal's Triangle(楊輝三角)LeetCode
- 【LeetCode從零單排】No58.Length of Last WordLeetCodeAST
- 【LeetCode從零單排】No67.AddBinaryLeetCode
- 【LeetCode從零單排】No70.ClimbingStairsLeetCodeAI
- 【LeetCode從零單排】No.9 Palindrome NumberLeetCode
- 【LeetCode從零單排】No14.LongestCommonPrefixLeetCode
- 【LeetCode從零單排】No36 Valid SudokuLeetCode
- 【LeetCode從零單排】No221.Maximal SquareLeetCode
- 每天一道LeetCode--119.Pascal's Triangle II(楊輝三角)LeetCode
- 【LeetCode從零單排】No15 3SumLeetCode
- 【LeetCode從零單排】No189 .Rotate ArrayLeetCode
- 【LeetCode從零單排】No88.Merge Sorted ArrayLeetCode
- Mysql從零單排-1MySql
- 【LeetCode從零單排】No96 Unique Binary Search TreesLeetCode
- 【LeetCode從零單排】No112 Path SumLeetCode
- 【LeetCode從零單排】No.169 Majority Element(hashmap用法)LeetCodeHashMap
- 【LeetCode從零單排】No83 Remove Duplicates from Sorted ListLeetCodeREM
- 【LeetCode從零單排】No26.Remove Duplicates from Sorted ArrayLeetCodeREM
- 從零單排學Redis【白銀】Redis
- ZOJ Monthly, January 2019 - A Little Sub and Pascal's Triangle(找規律)
- 【LeetCode從零單排】No 3 Longest Substring Without Repeating CharactersLeetCode
- 【LeetCode從零單排】No129 Sum Root to Leaf NumbersLeetCode
- 【LeetCode從零單排】No.160 Intersection of Two Linked ListsLeetCode
- 從零單排學Redis【黃金】Redis