GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode-3/problems/src/array/PascalsTriangle.java at master · abcdvemm/leetcode-3

Expand file tree

/

PascalsTriangle.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
44 lines (41 loc) · 1.06 KB
/

PascalsTriangle.java

Copy path

File metadata and controls

44 lines (41 loc) · 1.06 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by gouthamvidyapradhan on 25/03/2017.
*
* <p>Given an index k, return the kth row of the Pascal's triangle.
*
* <p>For example, given k = 3, Return [1,3,3,1].
*
* <p>Note: Could you optimize your algorithm to use only O(k) extra space?
*/
public class PascalsTriangle {
public static void main(String[] args) throws Exception {
System.out.println(new PascalsTriangle().getRow(3));
}
public List<Integer> getRow(int rowIndex) {
int k = rowIndex;
if (k == 0) return Arrays.asList(1);
else if (k == 1) return Arrays.asList(1, 1);
else if (k == 2) return Arrays.asList(1, 2, 1);
List<Integer> result = new ArrayList<>();
result.add(2);
k = k - 2;
int p, c;
while (k-- > 0) {
p = 1;
int i = 0;
for (int l = result.size(); i < l; i++) {
c = result.get(i);
result.set(i, p + c);
p = c;
}
result.add(p + 1);
}
result.add(0, 1);
result.add(1);
return result;
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.