The title
Type of trigonometric association: array
Given a non-negative integer numRows, generate the former numRows of the Yanghui triangle. In Yang Hui's triangle, each number is the sum of the numbers on its upper left and upper right. Example: input: 5 output: [[1], [1, 1], [1, 2, 1],,3,3,1 [1], [1,4,6,4,1]]Copy the code
Time to solve the problem.
class Solution {
public List<List<Integer>> generate(int numRows) {
}
}
Copy the code
The method input parameters are given above to complete the answer.
Subject analysis
- A double loop is used, with the outer loop for each row and the inner loop for each number of users in each row
- It’s going to be 1 at the beginning and 1 at the end
- The other positions use the left and right sides of the previous row to add and assign
Answers to analysis
This article only analysis I do the idea, only for reference, to understand a solution to the idea, other kinds of ideas to do the problem please access the Internet.
Answer successful: Execution time :0 ms, beat 100.00% Java user memory consumption :36.2 MB, beat 77% Java users
class Solution { public List<List<Integer>> generate(int numRows) { if (numRows == 0) { return null; } List<List<Integer>> outList = new ArrayList<>(); for (int i = 1; i < numRows + 1; i++) { List<Integer> list = new ArrayList<>(); for (int j = 0; j < i; J++) {if (j = = 0 | | j = = I - 1) {/ / fore and aft are 1 list. Add (1); } else {int c = outlist.get (i-2).get(j-1) + outlist.get (i-2).get(j); list.add(c); } } outList.add(list); } return outList; }}Copy the code