Reverse Integer
public class Lc7 {
public static int reverse(int x) {
boolean negativeNumberFlag = false;
if (x < 0) {
negativeNumberFlag = true;
x *= -1;
}
int len = 0;
int temp = x;
while (temp > 0) {
temp /= 10;
len++;
}
int[] array = new int[len];
for (int i = 0; i < len; i++) {
array[i] = (int) (x % Math.pow(10, i + 1) / Math.pow(10, i));
}
long digital = 0;
for (int i = array.length - 1, j = 0; i >= 0; i--, j++) {
digital += array[i] * Math.pow(10, j);
}
digital = negativeNumberFlag == true ? digital * -1 : digital;
return digital > Integer.MAX_VALUE || digital < Integer.MIN_VALUE ? 0 : (int) digital;
}
public static void main(String[] args) {
int digit = 1534236469;
System.out.println(reverse(digit));
}
}
Compare Version Numbers
/**
*
* Compare two version numbers version1 and version2. If version1 > version2
* return 1; if version1 < version2 return -1;otherwise return 0.
*
* You may assume that the version strings are non-empty and contain only digits
* and the . character.
*
* The . character does not represent a decimal point and is used to separate
* number sequences.
*
* For instance, 2.5 is not "two and a half" or "half way to version three", it
* is the fifth second-level revision of the second first-level revision.
*
* You may assume the default revision number for each level of a version number
* to be 0. For example, version number 3.4 has a revision number of 3 and 4 for
* its first and second level revision number. Its third and fourth level
* revision number are both 0.
*/
public class Lc165 {
public static int compareVersion(String version1, String version2) {
int p1 = 0, p2 = 0;
while (p1 < version1.length() || p2 < version2.length()) {
int num1 = 0, num2 = 0;
while (p1 < version1.length() && version1.charAt(p1) !=
num1 = num1 * 10 + (version1.charAt(p1++) -
while (p2 < version2.length() && version2.charAt(p2) !=
num2 = num2 * 10 + (version2.charAt(p2++) -
if (num1 != num2)
return num1 > num2 ? 1 : -1;
p1++;
p2++;
}
return 0;
}
public static void main(String[] args) {
String str1 = "1.0";
String str2 = "1";
System.out.println(compareVersion(str1, str2));
}
}