前言

刷leetcode有时候一些细小功能总是查了又忘,忘了又查,还不如总结加以刻意练习来达到掌握的效果。

数值

字符和ASCII码转换

char ch = 'A';
int ascii = (int) ch;
System.out.println("ASCII of " + ch + " is: " + ascii); // 输出 65

int ascii = 65;
char ch = (char) ascii;
System.out.println("Character for ASCII " + ascii + " is: " + ch); // 输出 A

字母大小写转换

str.toLowerCase();//使用toLowerCase()方法实现小写转换
str.toUpperCase();//使用toUpperCase()方法实现大写转换

判断字符串是否为字母或数字

1、 调用isDigit方法

char a = 'c';
char b = '2';
System.out.println(Character.isDigit(a));	// false
System.out.println(Character.isDigit(b));	// true

2、大小进行判断

char a = 'c';
char b = '2';
System.out.println(a >= '0' && a <= '9');
System.out.println(b >= '0' && b <= '9');

序列容器

数组

二维数组排序

int[][] array = {{5, 2}, {1, 7}, {5, 1}, {2, 9}};

Arrays.sort(array, (o1, o2) -> {
    // 先比较第一列
    if (o1[0] != o2[0]) {
        return o1[0] - o2[0]; // 升序
    } else {
        // 如果第一列相同,则比较第二列
        return o1[1] - o2[1]; // 升序
    }
});

数组转list

int[] b = new int[]{3,8,20,7,11,25};
Integer[] boxB = Arrays.stream(b).boxed().toArray(Integer[]::new);

列表

列表转数组
list.toArray(new String[0]) 这个方法是用来将一个 List 集合转换为 String 数组的。new String[0] 创建了一个长度为0的String数组,这个数组的作用是指定返回数组的类型和泛型,实际上返回的数组长度会根据List集合中元素的个数自动调整。

int[] arr = list.stream().mapToInt(Integer::intValue).toArray;
// 也可以用下面的语句
int[] arr = list.stream().mapToInt(Integer::valueOf).toArray;
切片

双端队列

优先队列

列表

排序

映射容器

默认字典value值

针对是数值类型

Map<Character, Integer> tCharNums = new HashMap<>();
 tCharNums.merge(_t, 1, Integer::sum);

针对是列表类型

Map<Integer, List<Integer>> outDegreeDetails = new HashMap<>(numCourses);
outDegreeDetails.computeIfAbsent(out, val -> new ArrayList<>()).add(in);

后记

持续学习!我爱学习!!!