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.
Just did a similar solution in C++. I must to be quite efficient
inline void patch_buffer(std::string & buffer, int i) {
// replace the i-th character of a buffer with a dash
buffer[i] = '#';
}
void staircase(int n) {
// fill the buffer with spaces
std::string buffer = std::string(n, ' ');
const int max_index = n - 1;
for(int i = max_index; i >= 0; i--) {
// for each line the (max_index - i)-th character of
// a buffer will be replaced with a dash
patch_buffer(buffer, i);
// and then the whole line will be printed
std::cout << buffer << "\n";
}
}
People that want to try something special may try to write an iterator with one range-based printing loop.
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 →
Just did a similar solution in C++. I must to be quite efficient
People that want to try something special may try to write an iterator with one range-based printing loop.