1
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>JavaBasiceDemo</artifactId>
|
||||
<groupId>com.wyl.example</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.wyl</groupId>
|
||||
<artifactId>leetcode</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.wyl.leetcode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class YangHuiTriangle {
|
||||
/**
|
||||
* 118.给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。
|
||||
* 在「杨辉三角」中,每个数是它左上方和右上方的数的和。
|
||||
* <p>
|
||||
* 示例 1:
|
||||
* <p>
|
||||
* 输入: numRows = 5
|
||||
* 输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
|
||||
* 示例 2:
|
||||
* 输入: numRows = 1
|
||||
* 输出: [[1]]
|
||||
* <p>
|
||||
* 提示:
|
||||
* <p>
|
||||
* 1 <= numRows <= 30
|
||||
*/
|
||||
public static List<List<Integer>> generate(int numRows) {
|
||||
int[][] res = new int[numRows][numRows];
|
||||
List<List<Integer>> resList = new ArrayList<>();
|
||||
for (int i = 0; i < numRows; i++) {
|
||||
List<Integer> row = new ArrayList<>();
|
||||
for (int j = 0; j < i + 1; j++) {
|
||||
if (i == 0 || j == 0 || i == j) {
|
||||
res[i][j] = 1;
|
||||
row.add(1);
|
||||
} else {
|
||||
res[i][j] = res[i - 1][j - 1] + res[i - 1][j];
|
||||
row.add(res[i][j]);
|
||||
}
|
||||
}
|
||||
resList.add(row);
|
||||
}
|
||||
return resList;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(25 >> 3);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user