-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPattern1.java
58 lines (49 loc) Β· 1.25 KB
/
Pattern1.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package day2;
import java.util.Scanner;
public class Pattern1 {
/*
rows = 4
#
##
###
####
rows = 2
0: #
1: ##
rows : n
ith row: i + 1 characters
*/
public static void main(String[] args) {
/*
For All pattern printing questions
for (int row = 0 ; row < rows ; row++) {
code for ith row
System.out.println();
}
*/
/*
n times
for (int var = 0 ; var < n ; var++) {}
*/
// Time Complexity: O()
Scanner scanner = new Scanner(System.in);
int rows = scanner.nextInt();
// rows = 3
/*
1 + 2 + 3 + 4 + .... + rows = rows (rows + 1) / 2 = O(rows^2)
rows * rows / 2 = O(rows^2)
*/
for (int row = 0 ; row < rows ; row++) {
// 1. print hashtags
// Time Complexity: O(row) [0, rows)
// 1 2 3 4 ... row avg = row/2
// row/2 + k = O(rows)
for (int i = 0 ; i < row + 1 ; i++) {
System.out.print('#');
}
// 2. print new line
// k
System.out.println();
}
}
}