Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added src/.DS_Store
Binary file not shown.
6 changes: 2 additions & 4 deletions src/class001/LanguageConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ public static void sort(int[] arr, int l, int r) {
// 但只有这一下随机,才能在概率上把快速排序的时间复杂度收敛到O(n * logn)
int x = arr[l + (int) (Math.random() * (r - l + 1))];
partition(arr, l, r, x);
int left = first;
int right = last;
sort(arr, l, left - 1);
sort(arr, right + 1, r);
sort(arr, l, first - 1);
sort(arr, last + 1, r);
}

public static int first, last;
Expand Down
222 changes: 111 additions & 111 deletions src/class009/ListReverse.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,116 +5,116 @@
// 以堆栈视角来看链表反转
public class ListReverse {

public static void main(String[] args) {
// int、long、byte、short
// char、float、double、boolean
// 还有String
// 都是按值传递
int a = 10;
f(a);
System.out.println(a);

// 其他类型按引用传递
// 比如下面的Number是自定义的类
Number b = new Number(5);
g1(b);
System.out.println(b.val);
g2(b);
System.out.println(b.val);

// 比如下面的一维数组
int[] c = { 1, 2, 3, 4 };
g3(c);
System.out.println(c[0]);
g4(c);
System.out.println(c[0]);
}

public static void f(int a) {
a = 0;
}

public static class Number {
public int val;

public Number(int v) {
val = v;
}
}

public static void g1(Number b) {
b = null;
}

public static void g2(Number b) {
b.val = 6;
}

public static void g3(int[] c) {
c = null;
}

public static void g4(int[] c) {
c[0] = 100;
}

// 单链表节点
public static class ListNode {
public int val;
public ListNode next;

public ListNode(int val) {
this.val = val;
}

public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}

// 反转单链表测试链接 : https://leetcode.cn/problems/reverse-linked-list/
class Solution {

public static ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode next = null;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}

}

// 双链表节点
public static class DoubleListNode {
public int value;
public DoubleListNode last;
public DoubleListNode next;

public DoubleListNode(int v) {
value = v;
}
}

// 反转双链表
// 没有找到测试链接
// 如下方法是对的
public static DoubleListNode reverseDoubleList(DoubleListNode head) {
DoubleListNode pre = null;
DoubleListNode next = null;
while (head != null) {
next = head.next;
head.next = pre;
head.last = next;
pre = head;
head = next;
}
return pre;
}
public static void main(String[] args) {
// int、long、byte、short
// char、float、double、boolean
// 还有String
// 都是按值传递
int a = 10;
f(a);
System.out.println(a);

// 其他类型按引用传递
// 比如下面的Number是自定义的类
Number b = new Number(5);
g1(b);
System.out.println(b.val);
g2(b);
System.out.println(b.val);

// 比如下面的一维数组
int[] c = {1, 2, 3, 4};
g3(c);
System.out.println(c[0]);
g4(c);
System.out.println(c[0]);
}

public static void f(int a) {
a = 0;
}

public static class Number {
public int val;

public Number(int v) {
val = v;
}
}

public static void g1(Number b) {
b = null;
}

public static void g2(Number b) {
b.val = 6;
}

public static void g3(int[] c) {
c = null;
}

public static void g4(int[] c) {
c[0] = 100;
}

// 单链表节点
public static class ListNode {
public int val;
public ListNode next;

public ListNode(int val) {
this.val = val;
}

public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}

// 反转单链表测试链接 : https://leetcode.cn/problems/reverse-linked-list/
class Solution {

public static ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode next = null;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}

}

// 双链表节点
public static class DoubleListNode {
public int value;
public DoubleListNode last;
public DoubleListNode next;

public DoubleListNode(int v) {
value = v;
}
}

// 反转双链表
// 没有找到测试链接
// 如下方法是对的
public static DoubleListNode reverseDoubleList(DoubleListNode head) {
DoubleListNode pre = null;
DoubleListNode next = null;
while (head != null) {
next = head.next;
head.next = pre;
head.last = next;
pre = head;
head = next;
}
return pre;
}

}
8 changes: 2 additions & 6 deletions src/class023/Code01_QuickSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,8 @@ public static void quickSort2(int l, int r) {
// 但只有这一下随机,才能在概率上把快速排序的时间复杂度收敛到O(n * logn)
int x = arr[l + (int) (Math.random() * (r - l + 1))];
partition2(l, r, x);
// 为了防止底层的递归过程覆盖全局变量
// 这里用临时变量记录first、last
int left = first;
int right = last;
quickSort2(l, left - 1);
quickSort2(right + 1, r);
quickSort2(l, first - 1);
quickSort2(last + 1, r);
}

// 荷兰国旗问题
Expand Down
8 changes: 2 additions & 6 deletions src/class023/Code02_QuickSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,8 @@ public static void quickSort2(int[] arr, int l, int r) {
// 但只有这一下随机,才能在概率上把快速排序的时间复杂度收敛到O(n * logn)
int x = arr[l + (int) (Math.random() * (r - l + 1))];
partition2(arr, l, r, x);
// 为了防止底层的递归过程覆盖全局变量
// 这里用临时变量记录first、last
int left = first;
int right = last;
quickSort2(arr, l, left - 1);
quickSort2(arr, right + 1, r);
quickSort2(arr, l, first - 1);
quickSort2(arr, last + 1, r);
}

// 荷兰国旗问题
Expand Down
3 changes: 0 additions & 3 deletions src/class024/RandomizedSelect.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ public static int randomizedSelect(int[] arr, int i) {
// 随机这一下,常数时间比较大
// 但只有这一下随机,才能在概率上把时间复杂度收敛到O(n)
partition(arr, l, r, arr[l + (int) (Math.random() * (r - l + 1))]);
// 因为左右两侧只需要走一侧
// 所以不需要临时变量记录全局的first、last
// 直接用即可
if (i < first) {
r = first - 1;
} else if (i > last) {
Expand Down
Loading