今天的题目有
- 数组
- 求一个数组中的第 k 小 / 大的数
- 求所有子数组的和的最大值。
- 字符串
- 输入一个字符串,输出该字符串中字符的所有组合。
- 字符串的排列问题,例如"abc"可以排多少个出来
- 给定一个字符串,请你将字符串重新编码,将连续的字符替换成“连续出现的个数+字符”。比如字符串AAAABCCDAA会被编码成4A1B2C1D2A。(来源网易游戏笔试)
- 链表
总结
- 快速排序的思路从后往前找到小于基准(即是说明等于是不满足的,前面也是)
- 递归:要明白每个变量代表的是什么,三部法:函数的作用、结束的条件、循环体
- 字符串组合使用树的方式进行思考(每个元素取或不取)
- 类型转行
- 注意:不要对char[] chars 使用 chars.toString() 转为String类型。char的包装类:Character
- String str -> char[] chars : chars = str.toCharArray();
- char[] chars -> String str : str = String.valueOf(chars);
- char chars[i] -> String str : str = String.valueOf(chars[i]);
- 获取输入使用Scanner sc = new Scanner(System.in); String str = sc.next();
数组
求一个数组中的第 k 小 / 大的数
public static int singleNum(int[] array, int K) {
if (K < 1) {
return -1;
}
quickSort(array, 0, array.length - 1);
System.out.println(Arrays.toString(array));
if (K == 1) {
return array[array.length - 1];
}
int count = 1;
for (int i = array.length - 1; i >= 0; i--) {
if (array[i] == array[i - 1]) {
continue;
} else {
count++;
if (count == K) {
return array[i - 1];
}
}
}
return -1;
}
public static int[] quickSort(int[] array, int low, int high) {
if (array == null) {
return null;
}
int start = low;
int end = high;
int base = array[start];
while (end > start) {
while (start < end && array[end] >= base) {
end--;
}
if (array[end] < base) {
int temp = array[start];
array[start] = array[end];
array[end] = temp;
}
while (start < end && array[start] < base) {
start++;
}
if (array[start] >= base) {
int temp = array[end];
array[end] = array[start];
array[start] = temp;
}
}
if (start > low) {
quickSort(array, low, start - 1);
}
if (end < high) {
quickSort(array, end + 1, high);
}
return array;
}
求所有子数组的和的最大值。
public static int FindGreatestSumOfSubArray(int[] array) {
if (array == null) {
return -1;
}
int res = array[0];
int max = array[0];
for (int i = 1; i < array.length; i++) {
max = Math.max(max + array[i], array[i]);
res = Math.max(res, max);
}
return res;
}
字符串
输入一个字符串,输出该字符串中字符的所有组合。
private static void printAllSubString(String str) {
if (str == null || str == "") {
System.out.println("不存在子字符串");
return;
}
char[] chars = str.toCharArray();
String currentStr = new String("");
printAllSubString(0, currentStr, chars);
}
private static void printAllSubString(int count, String currentStr, char[] chars) {
if (count == chars.length) {
System.out.println(currentStr);
return;
}
printAllSubString(count + 1, currentStr + String.valueOf(chars[count]), chars);
printAllSubString(count + 1, currentStr, chars);
}
字符串的排列问题,例如"abc"可以排多少个出来
public static ArrayList<String> Permutation(String str) {
if(str == null || str == ""){
return null;
}
List<String> list = new ArrayList<>();
char[] chars = str.toCharArray();
Permutation(0, list, chars);
return (ArrayList) list;
}
public static void Permutation(int i, List list, char[] chars) {
if(i == chars.length - 1) {
String str = String.valueOf(chars);
if(!list.contains(str)){
list.add(str);
}
return;
}
for(int j=i; j<chars.length; j++){
swap(chars, i, j);
Permutation(i+1, list, chars);
swap(chars, i, j);
}
}
public static void swap(char[] chars, int i, int j){
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
给定一个字符串,请你将字符串重新编码,将连续的字符替换成“连续出现的个数+字符”。比如字符串AAAABCCDAA会被编码成4A1B2C1D2A。
public static void recodeToString(){
Scanner sc = new Scanner(System.in);
String str = sc.next();
char[] chars = str.toCharArray();
if (chars.length <= 1){
if (chars.length == 1){
System.out.print(1+""+chars[0]);
}
System.out.print("");
return;
}
int count = 1;
for (int i = 1; i < chars.length; i++) {
if (chars[i] == chars[i - 1]) {
count++;
} else {
System.out.print(count);
System.out.print(chars[i-1] + "");
count = 1;
}
if (i== chars.length-1) {
System.out.print(count);
System.out.print(chars[i] + "");
}
}
}
链表
反转链表(递归实现)
public ListNode ReverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode newNode = ReverseList(head.next);
head.next.next = head;
head.next = null;
return newNode;
}