这天小明正在学数数 他突然发现有些正整数的形状像一座“山”,比如 123565321、145541,它们左右对称(回文)且数位上的数字先单调不减,后单调不增小明数了很久也没有数完,他想让你告诉他在区间 [2023,2023232023]中有 多少个数的形状像一座“山”。
public class Main {
public static void main(String[] args) {
long start = System.currentTimeMillis();
int count = 0;
for (int i = 2022; i <= 2023322023; i++) {
if (isPalindrome(i) && check(i)) {
count++;
}
}
System.out.println(count);
}
private static boolean check(int num) {
String s = num + "";
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) > s.charAt(i + 1)) return false;
}
return true;
}
private static boolean isPalindrome(int num) {
String s = num + "";
int n = s.length() - 1;
for (int l = 0, r = n; l < r; l++, r --)
if (s.charAt(l) != s.charAt(r)) return false;
return true;
}
}