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.
This problem reminds me with a similar problem that I faced during the early semesters of my Bachelor study :)
At that time, I used the inefficient nested loop solutions. Turns out, there's a way better solution posted here. Thanks!
Here's my solution (inspired by your solution):
importjava.io.*;importjava.util.*;importjava.text.*;importjava.math.*;importjava.util.regex.*;publicclassSolution{staticvoidstaircase(intn){// Complete this functionStringBuildersb=newStringBuilder();//create the spacesfor(inti=0;i<n-1;i++){sb.append(" ");}//create the pound signs (replace the spaces to the pound sign)for(intj=n;j>0;j--){sb.replace(j-1,j,"#");System.out.println(sb);}}publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);intn=in.nextInt();staircase(n);in.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Staircase
You are viewing a single comment's thread. Return to all comments →
This problem reminds me with a similar problem that I faced during the early semesters of my Bachelor study :) At that time, I used the inefficient nested loop solutions. Turns out, there's a way better solution posted here. Thanks!
Here's my solution (inspired by your solution):