-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPattern2.java
51 lines (41 loc) Β· 970 Bytes
/
Pattern2.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
package day2;
import java.util.Scanner;
public class Pattern2 {
/*
n = 4
___*
__**
_***
****
n = 2
_*
**
spaces: n - i - 1
f : 1 2 3 4 5 ....
i : 0 1 2 3 4
stars: i + 1
newline: 1
*/
/*
Time Complexity: O(n^2)
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0 ; i < n ; i++) {
// total: n + k O(n)
// print spaces
// time complexity: O(n - i - 1)
for (int j = 0 ; j < n - i - 1 ; j++) {
System.out.print(' ');
}
// print star
// time complexity: O(i + 1)
for (int j = 0 ; j < i + 1 ; j++) {
System.out.print('*');
}
// new line
System.out.println();
}
}
}