Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

##Algorithm

Algorithm

Your time and complexity problems are probably the more serious ones. Your code starts small (at 100), and then works up to 1000. This is not the most efficient way of brute-forcing the problem. A better solution would be to start at 999 and work back to 100, but limit it when the result becomes impossible.... let me explain:

 int max = Integer.MIN_VALUE;
 for (int i = 999; i > 100 && i * 999 > max; i--) {
 for (int j = 999; j > i; j--) {
 ....
  1. start high (999 x 999)
  2. Work backwards from there where the second number is larger than the first
  3. track your maximum
  4. terminate the loops if the current i (or smaller) values will never beat the maximum.

Using the above logic, you will eliminate a huge number of loops.

##Length check & Finding digit

Length check & Finding digit

The Log function is a good check for the length of the value, it's fine. On the other hand, it may not be necessary at all... same applies to the "finding digit"

##Palindrome

Palindrome

I like the following method for identifying palindromic numbers:

private static final int reverse(int value) {
 int result = 0;
 while (value != 0) {
 result *= 10;
 result += value % 10;
 value /= 10;
 }
 return result;
}

Using that function, your palindrome check simply becomes:

boolean ispalindrome = n == reverse(n);

##Algorithm

Your time and complexity problems are probably the more serious ones. Your code starts small (at 100), and then works up to 1000. This is not the most efficient way of brute-forcing the problem. A better solution would be to start at 999 and work back to 100, but limit it when the result becomes impossible.... let me explain:

 int max = Integer.MIN_VALUE;
 for (int i = 999; i > 100 && i * 999 > max; i--) {
 for (int j = 999; j > i; j--) {
 ....
  1. start high (999 x 999)
  2. Work backwards from there where the second number is larger than the first
  3. track your maximum
  4. terminate the loops if the current i (or smaller) values will never beat the maximum.

Using the above logic, you will eliminate a huge number of loops.

##Length check & Finding digit

The Log function is a good check for the length of the value, it's fine. On the other hand, it may not be necessary at all... same applies to the "finding digit"

##Palindrome

I like the following method for identifying palindromic numbers:

private static final int reverse(int value) {
 int result = 0;
 while (value != 0) {
 result *= 10;
 result += value % 10;
 value /= 10;
 }
 return result;
}

Using that function, your palindrome check simply becomes:

boolean ispalindrome = n == reverse(n);

Algorithm

Your time and complexity problems are probably the more serious ones. Your code starts small (at 100), and then works up to 1000. This is not the most efficient way of brute-forcing the problem. A better solution would be to start at 999 and work back to 100, but limit it when the result becomes impossible.... let me explain:

 int max = Integer.MIN_VALUE;
 for (int i = 999; i > 100 && i * 999 > max; i--) {
 for (int j = 999; j > i; j--) {
 ....
  1. start high (999 x 999)
  2. Work backwards from there where the second number is larger than the first
  3. track your maximum
  4. terminate the loops if the current i (or smaller) values will never beat the maximum.

Using the above logic, you will eliminate a huge number of loops.

Length check & Finding digit

The Log function is a good check for the length of the value, it's fine. On the other hand, it may not be necessary at all... same applies to the "finding digit"

Palindrome

I like the following method for identifying palindromic numbers:

private static final int reverse(int value) {
 int result = 0;
 while (value != 0) {
 result *= 10;
 result += value % 10;
 value /= 10;
 }
 return result;
}

Using that function, your palindrome check simply becomes:

boolean ispalindrome = n == reverse(n);
deleted 1 character in body
Source Link
maaartinus
  • 13.6k
  • 1
  • 35
  • 74

##Algorithm

Your time and complexity problems are probably the more serious ones. Your code starts small (at 100), and then works up to 1000. This is not the most efficient way of brute-forcing the problem. A better solution would be to start at 999 and work back to 100, but limit it when the result becomes impossible.... let me explain:

 int max = Integer.MIN_VALUE;
 for (int i = 999; i > 100 && i * 999 > max; i--}) ({
 for (int j = 1000;999; j > i; j--) {
 ....
  1. start high (999 x 999)
  2. Work backwards from there where the second number is larger than the first
  3. track your maximum
  4. terminate the loops if the current i (or smaller) values will never beat the maximum.

Using the above logic, you will eliminate a huge number of loops.

##Length check & Finding digit

The Log function is a good check for the length of the value, it's fine. On the other hand, it may not be necessary at all... same applies to the "finding digit"

##Palindrome

I like the following method for identifying palindromic numbers:

private static final int reverse(int value) {
 int result = 0;
 while (value != 0) {
 result *= 10;
 result += value % 10;
 value /= 10;
 }
 return result;
}

Using that function, your palindrome check simply becomes:

boolean ispalindrome = n == reverse(n);

##Algorithm

Your time and complexity problems are probably the more serious ones. Your code starts small (at 100), and then works up to 1000. This is not the most efficient way of brute-forcing the problem. A better solution would be to start at 999 and work back to 100, but limit it when the result becomes impossible.... let me explain:

 int max = Integer.MIN_VALUE;
 for (int i = 999; i > 100 && i * 999 > max; i--} (
 for (int j = 1000; j > i; j--) {
 ....
  1. start high (999 x 999)
  2. Work backwards from there where the second number is larger than the first
  3. track your maximum
  4. terminate the loops if the current i (or smaller) values will never beat the maximum.

Using the above logic, you will eliminate a huge number of loops.

##Length check & Finding digit

The Log function is a good check for the length of the value, it's fine. On the other hand, it may not be necessary at all... same applies to the "finding digit"

##Palindrome

I like the following method for identifying palindromic numbers:

private static final int reverse(int value) {
 int result = 0;
 while (value != 0) {
 result *= 10;
 result += value % 10;
 value /= 10;
 }
 return result;
}

Using that function, your palindrome check simply becomes:

boolean ispalindrome = n == reverse(n);

##Algorithm

Your time and complexity problems are probably the more serious ones. Your code starts small (at 100), and then works up to 1000. This is not the most efficient way of brute-forcing the problem. A better solution would be to start at 999 and work back to 100, but limit it when the result becomes impossible.... let me explain:

 int max = Integer.MIN_VALUE;
 for (int i = 999; i > 100 && i * 999 > max; i--) {
 for (int j = 999; j > i; j--) {
 ....
  1. start high (999 x 999)
  2. Work backwards from there where the second number is larger than the first
  3. track your maximum
  4. terminate the loops if the current i (or smaller) values will never beat the maximum.

Using the above logic, you will eliminate a huge number of loops.

##Length check & Finding digit

The Log function is a good check for the length of the value, it's fine. On the other hand, it may not be necessary at all... same applies to the "finding digit"

##Palindrome

I like the following method for identifying palindromic numbers:

private static final int reverse(int value) {
 int result = 0;
 while (value != 0) {
 result *= 10;
 result += value % 10;
 value /= 10;
 }
 return result;
}

Using that function, your palindrome check simply becomes:

boolean ispalindrome = n == reverse(n);
added 7 characters in body
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396

##Algorithm

Your time and complexity problems are probably the more serious ones. Your code starts small (at 100), and then works up to 1000. This is not the most efficient way of brute-forcing the problem. A better solution would be to start at 999 and work back to 100, but limit it when the result becomes impossible.... let me explain:

 int max = Integer.MIN_VALUE;
 for (int i = 999; i > 100 && i * 999 > max; i--} (
 for (int j = 1000; j > i; j--) {
 ....
  1. start high (999 x 999)
  2. Work backwards from there where the second number is larger than the first
  3. track your maximum
  4. terminate the loops if the current i (or smaller) values will never beat the maximum.

Using the above logic, you will eliminate a huge number of loops.

##Length check & Finding digit

The Log function is a good check for the length of the value, it's fine. On the other hand, it may not be necessary at all... same applies to the "finding digit"

##Palindrome

I like the following method for identifying planindromespalindromic numbers:

private static final int reverse(int value) {
 int result = 0;
 while (value != 0) {
 result *= 10;
 result += value % 10;
 value /= 10;
 }
 return result;
}

Using that function, your palindrome check simply becomes:

boolean ispalindrome = n == reverse(n);

##Algorithm

Your time and complexity problems are probably the more serious ones. Your code starts small (at 100), and then works up to 1000. This is not the most efficient way of brute-forcing the problem. A better solution would be to start at 999 and work back to 100, but limit it when the result becomes impossible.... let me explain:

 int max = Integer.MIN_VALUE;
 for (int i = 999; i > 100 && i * 999 > max; i--} (
 for (int j = 1000; j > i; j--) {
 ....
  1. start high (999 x 999)
  2. Work backwards from there where the second number is larger than the first
  3. track your maximum
  4. terminate the loops if the current i (or smaller) values will never beat the maximum.

Using the above logic, you will eliminate a huge number of loops.

##Length check & Finding digit

The Log function is a good check for the length of the value, it's fine. On the other hand, it may not be necessary at all... same applies to the "finding digit"

##Palindrome

I like the following method for identifying planindromes:

private static final int reverse(int value) {
 int result = 0;
 while (value != 0) {
 result *= 10;
 result += value % 10;
 value /= 10;
 }
 return result;
}

Using that function, your palindrome check simply becomes:

boolean ispalindrome = n == reverse(n);

##Algorithm

Your time and complexity problems are probably the more serious ones. Your code starts small (at 100), and then works up to 1000. This is not the most efficient way of brute-forcing the problem. A better solution would be to start at 999 and work back to 100, but limit it when the result becomes impossible.... let me explain:

 int max = Integer.MIN_VALUE;
 for (int i = 999; i > 100 && i * 999 > max; i--} (
 for (int j = 1000; j > i; j--) {
 ....
  1. start high (999 x 999)
  2. Work backwards from there where the second number is larger than the first
  3. track your maximum
  4. terminate the loops if the current i (or smaller) values will never beat the maximum.

Using the above logic, you will eliminate a huge number of loops.

##Length check & Finding digit

The Log function is a good check for the length of the value, it's fine. On the other hand, it may not be necessary at all... same applies to the "finding digit"

##Palindrome

I like the following method for identifying palindromic numbers:

private static final int reverse(int value) {
 int result = 0;
 while (value != 0) {
 result *= 10;
 result += value % 10;
 value /= 10;
 }
 return result;
}

Using that function, your palindrome check simply becomes:

boolean ispalindrome = n == reverse(n);
Source Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /