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.
I'll add my voice in favor of using a StringBuilder, although I admit not in the way that maxinshen chose to use it.
The problem with SergioGM's suggestion is that writing to the output buffer one character at a time can really slow things down, because it will get flushed after each of those write operations. There's challenges on HR where trying that will result in a TE.
With yli131's suggestion, you'd be slowing things down, because ironically the compiler will use a StringBuilder to perform those concatinations. It's slower overally because it will need the substrings first, so it produces wholly new String objects for each one, and then throws them away.
My own suggest is to move the sb instantiation outside of the for(int j ...) loop, modify sb as needed, and then after the inner loop finishes write the new value back to grid[i].
One other thing...
Instead of doing a somewhat expensive and unnecessary function call with .getNumericValue(), you can convert the digits yourself. To do that, you take advantage of the fact that chars are just numbers under the hood, and that the input format for this challenge uses simple ASCII. That means the digits 0 to 9 have sequential integer values, and so '3' - '0' == 3. If you're curious, you can click through the source here.
Hope some of that was interesting and useful!
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cavity Map
You are viewing a single comment's thread. Return to all comments →
I'll add my voice in favor of using a StringBuilder, although I admit not in the way that maxinshen chose to use it.
The problem with SergioGM's suggestion is that writing to the output buffer one character at a time can really slow things down, because it will get flushed after each of those write operations. There's challenges on HR where trying that will result in a TE.
With yli131's suggestion, you'd be slowing things down, because ironically the compiler will use a StringBuilder to perform those concatinations. It's slower overally because it will need the substrings first, so it produces wholly new String objects for each one, and then throws them away.
My own suggest is to move the
sb
instantiation outside of thefor(int j ...)
loop, modifysb
as needed, and then after the inner loop finishes write the new value back togrid[i]
.One other thing...
Instead of doing a somewhat expensive and unnecessary function call with
.getNumericValue()
, you can convert the digits yourself. To do that, you take advantage of the fact thatchar
s are just numbers under the hood, and that the input format for this challenge uses simple ASCII. That means the digits 0 to 9 have sequential integer values, and so'3' - '0' == 3
. If you're curious, you can click through the source here.Hope some of that was interesting and useful!