We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
// Use Java version 15.
+ First, I realized that the "#" character should be printed from 1 up to n each line.
+ Therefore, the number of spaces printed on each line equals n minus the number of "#" characters.
+ To reduce the number of System.out.println calls (which improves I/O effiency) we can use the StringBuilder with the repeat() method (available since Java 11).
// Source Code:
public static void staircase(int n) {
// Write your code here
StringBuilder sb = new StringBuilder();
for (int i = 0 ; i< n ; i++) {
Staircase
You are viewing a single comment's thread. Return to all comments →
// Use Java version 15. + First, I realized that the "#" character should be printed from 1 up to n each line. + Therefore, the number of spaces printed on each line equals n minus the number of "#" characters. + To reduce the number of System.out.println calls (which improves I/O effiency) we can use the StringBuilder with the repeat() method (available since Java 11).
// Source Code: public static void staircase(int n) { // Write your code here StringBuilder sb = new StringBuilder(); for (int i = 0 ; i< n ; i++) {