整理自https://cloud.tencent.com/developer/article/1237641
1、回文数
public static boolean palindrom(Integer integer) {
String str1 = String.valueOf(integer);
String str2 = new StringBuilder(str1).reverse().toString();
return str1.equals(str2);
}
public static void main(String[] args) {
System.out.println(palindrom(1232));
}
2、输入一行字符,分别统计出其英文字母、空格、数字和其它字符的个数。
方法1:正则替换
public class Test {
public static void main(String[] args) {
String str = new String("asd123asd");
System.out.println(abcNum(str));
}
public static int abcNum(String str) {
return str.length()-str.replaceAll("[a-zA-Z]","").length();
}
}
方法2:正则校验
public class Test {
public static void main(String[] args) {
String str = new String("asd123asd");
System.out.println(abcNum(str));
}
public static int abcNum(String str) {
int count = 0;
String regex = "[a-zA-Z]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
count++;
}
return count;
}
}
public static int numNum(String s) {
return s.length() - s.replaceAll("\\d", "").length();
}
public static int spaceNum(String s) {
return s.length() - s.replaceAll(" ", "").length();
}
public static int otherNum(String s) {
return s.length() - s.replaceAll("[^a-zA-Z\\d ]", "").length();
}
3、输入某年某月某日,判断这一天是这一年的第几天?
public static int dayOfYearByDateFormat(int year, int month, int day) {
SimpleDateFormat sdf = new SimpleDateFormat("DDD");
return Integer.parseInt(sdf.format(new Date(year, month - 1, day)));
}
public static int dayOfYearByCalendar(int year, int month, int day) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day);
return calendar.get(Calendar.DAY_OF_YEAR);
}
public static int dayOfYearByLocalDate(int year, int month, int day) {
LocalDate date = LocalDate.of(year, month, day);
return date.getDayOfYear();
}
4、数组排序
降序:
public static Integer[] sortByArrays(Integer... arr) {
Arrays.sort(arr,(x, y) -> y - x);
return arr;
}
升序
public static int[] sortByArrays(int... arr) {
Arrays.sort(arr);
return arr;
}
逆序:
public static Integer[] resversedInteger(Integer[] arr) {
List list = Arrays.asList(arr);
Collections.reverse(list);
return list.toArray(new Integer[arr.length]);
}
逆序:
public static int[] resversedArray(int[] arr) {
int[] ints = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
ints[i] = arr[arr.length - i - 1];
}
return ints;
}
public static int[] sort(int... arr) {
return IntStream.of(arr).sorted().toArray();
}
public static int[] reverseSort(int... arr) {
return IntStream.of(arr).boxed()
.sorted(Comparator.reverseOrder())
.mapToInt(e -> (int) e)
.toArray();
}
5、逆序输出数字并统计位数
public static String[] d(int num) {
String s = new StringBuilder(String.valueOf(num)).reverse().toString();
return new String[]{String.valueOf(s.length()), s};
}
6、数字截取指定位置
public static int intSub(int num, int start, int end) {
String s = String.valueOf(num);
String substring = s.substring(s.length() - end, s.length() - start + 1);
return Integer.parseInt(substring);
}
7、统计字符串中子串出现的次数
public static int strNum(String str, String s) {
int count = 0;
int i;
while ((i = str.indexOf(s)) >= 0) {
str = str.substring(i + s.length());
count++;
}
return count;
}
8、输入数组, 最大的与第一个元素交换,最小的与最后一个元素交换
public static Double[] startMaxEndMin(Double[] arr) {
Stream stream = Arrays.stream(arr);
Double max = stream.max(Double::compareTo).get();
Double min = stream.min(Double::compareTo).get();
int i = Arrays.asList(arr).indexOf(max);
arr[i] = arr[0];
arr[0] = max;
// 被交换了得重新转
int j = Arrays.asList(arr).indexOf(min);
arr[j] = arr[arr.length - 1];
arr[arr.length - 1] = max;
return arr;
}
9、素数判断
public static boolean testIsPrime3(int n) {
if (n <= 3) {
return n > 1;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
10、一个偶数总能表示为两个素数之和
public static int[] obb2Prime(int obb) {
if (obb % 2 != 0) return null;
for (int i = 2; i < obb / 2; i++) {
if (isPrime(i) && isPrime(obb - i)) {
return new int[]{i, obb - i};
}
}
return null;
}
11、将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
public static List factor(int num) {
List list = new ArrayList<>();
for (int i = 2; i <= num + 1; i++) {
while (num % i == 0 && num != i) {
list.add(i);
num /= i;
}
if (num == i) {
list.add(i);
break;
}
}
return list;
}
12、公约数 公倍数
公约数
public static int maxCommonDivisor(int a, int b) {
int temp = a;
if (a < b) {
a = b;
b = temp;
}
while (temp != 0) {
temp = a % b;
a = b;
b = temp;
}
return a;
}
公倍数
public static int minCommonMultiple(int a, int b) {
return a * b / maxCommonDivisor(a, b);
}
13、完数:一个数如果恰好等于它的因子之和
public static boolean isWholeNumber(int num) {
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) sum += i;
}
return sum == num;
}
14、水仙花数:各位数字立方和等于该数本身的三位数
public static boolean isNarcissisticNum(int num) {
int a = num / 100;
int b = (num / 10) % 10;
int c = num % 10;
int sum = a * a * a + b * b * b + c * c * c;
return sum == num;
}