ExactlyOriginally posted by: damiano
what do you mean reverse the digits?
like 12345 becomes 54321 ?
nice
I am trying to stay away from strings, heh.Originally posted by: agnitrate
As always recursion is the answer.
[edit] I read digits as strings for some reason. You could modify the above method if you desired, but there's probably a better way for just integers, although I would still probably use recursion. [/edit]
-silver
java.lang.StringBuffer is your friend.Originally posted by: MCrusty
I need to take a number and reverse the digits of it. But since I am using Java and Strings are immutable it is very inefficient to use a String to reverse the digits, is there some sort of algebraic way to do this...or am I just out of luck?
or the divide and mod method cited above would workOriginally posted by: FeathersMcGraw
java.lang.StringBuffer is your friend.Originally posted by: MCrusty
I need to take a number and reverse the digits of it. But since I am using Java and Strings are immutable it is very inefficient to use a String to reverse the digits, is there some sort of algebraic way to do this...or am I just out of luck?
Why? You're using Java, so it's not like memory efficiency is a big deal anyways. Unless you're specifically told NOT to use strings. I know that I had to do this last year because this was one of my lab exercises. I forget how exactly I implemented it but I know I used recursion because it was the easiest overall to implement. I found the above link by a simple google search 'string reverse recursion' so you'd probably get lots of hits by replacing string->integer. Good luck!Originally posted by: MCrusty
I am trying to stay away from strings, heh.Originally posted by: agnitrate
As always recursion is the answer.
[edit] I read digits as strings for some reason. You could modify the above method if you desired, but there's probably a better way for just integers, although I would still probably use recursion. [/edit]
-silver
mod by ten take the mod mutiply by ten the modOriginally posted by: damiano
or the divide and mod method cited above would workOriginally posted by: FeathersMcGraw
java.lang.StringBuffer is your friend.Originally posted by: MCrusty
I need to take a number and reverse the digits of it. But since I am using Java and Strings are immutable it is very inefficient to use a String to reverse the digits, is there some sort of algebraic way to do this...or am I just out of luck?
It's just as easily solvable with an iterative solution, particularly since you have a well-defined number of iterations.Originally posted by: agnitrate
As always recursion is the answer.
look above the mod and add method in a loop will be fasterOriginally posted by: MCrusty
Our program has to run in under 10 seconds and we have to do TONS of calculations. I am trying to make it as efficient as possible, and creating a new string everytime I append a new character to it, is the opposite of efficient.
Yeah, im starting to write that one out .Originally posted by: damiano
look above the mod and add method in a loop will be fasterOriginally posted by: MCrusty
Our program has to run in under 10 seconds and we have to do TONS of calculations. I am trying to make it as efficient as possible, and creating a new string everytime I append a new character to it, is the opposite of efficient.
StringBuffer does not create new String objects during the append process if you preallocate the appropriate storage first.Originally posted by: MCrusty
Our program has to run in under 10 seconds and we have to do TONS of calculations. I am trying to make it as efficient as possible, and creating a new string everytime I append a new character to it, is the opposite of efficient.
I would write it for youOriginally posted by: MCrusty
Yeah, im starting to write that one out .Originally posted by: damiano
look above the mod and add method in a loop will be fasterOriginally posted by: MCrusty
Our program has to run in under 10 seconds and we have to do TONS of calculations. I am trying to make it as efficient as possible, and creating a new string everytime I append a new character to it, is the opposite of efficient.
![]()
yeah this is a very typical problem.Originally posted by: ggavinmoss
This is pretty easy, think about it. Here's a hint, you'll use / and %.
-geoff
you dont need to store it in a string, just keep everything as primitive typesOriginally posted by: MCrusty
static long reverse(long a)
{
StringBuffer t = new StringBuffer();
while (a!=0)
{
t.append(a%10);
a/=10;
}
return Long.parseLong(t.toString());
}
How else would you store it? StringBuffers seem to be the most efficient way to store for something this trivial.Originally posted by: Ameesh
you dont need to store it in a string, just keep everything as primitive typesOriginally posted by: MCrusty
static long reverse(long a)
{
StringBuffer t = new StringBuffer();
while (a!=0)
{
t.append(a%10);
a/=10;
}
return Long.parseLong(t.toString());
}
The result can be accumulated as an integer/long type, as long as maintaining leading zeroes is not significant.Originally posted by: MCrusty
How else would you store it? StringBuffers seem to be the most efficient way to store for something this trivial.
absolutlyOriginally posted by: FeathersMcGraw
The result can be accumulated as an integer/long type, as long as maintaining leading zeroes is not significant.Originally posted by: MCrusty
How else would you store it? StringBuffers seem to be the most efficient way to store for something this trivial.