auto commit

This commit is contained in:
CyC2018 2018-06-30 14:11:16 +08:00
parent 7194925d41
commit 8568b121cb
3 changed files with 123 additions and 97 deletions

View File

@ -261,7 +261,7 @@ obj = null;
finalize() 类似 C++ 的析构函数,用来做关闭外部资源等工作。但是 try-finally 等方式可以做的更好,并且该方法运行代价高昂,不确定性大,无法保证各个对象的调用顺序,因此最好不要使用。 finalize() 类似 C++ 的析构函数,用来做关闭外部资源等工作。但是 try-finally 等方式可以做的更好,并且该方法运行代价高昂,不确定性大,无法保证各个对象的调用顺序,因此最好不要使用。
当一个对象可被回收时,如果需要执行该对象的 finalize() 方法,那么就有可能通过在该方法中让对象重新被引用,从而实现自救。 当一个对象可被回收时,如果需要执行该对象的 finalize() 方法,那么就有可能通过在该方法中让对象重新被引用,从而实现自救。自救只能进行一次,如果回收的对象之前调用了 finalize() 方法自救,后面回收时不会调用 finalize() 方法。
## 垃圾收集算法 ## 垃圾收集算法

View File

@ -130,6 +130,8 @@ info 与 man 类似,但是 info 将文档分成一个个页面,每个页面
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/dmtsai/.local/bin:/home/dmtsai/bin /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/dmtsai/.local/bin:/home/dmtsai/bin
``` ```
env 命令可以获取当前终端的环境变量。
## sudo ## sudo
sudo 允许一般用户使用 root 可执行的命令,不过只有在 /etc/sudoers 配置文件中添加的用户才能使用该指令。 sudo 允许一般用户使用 root 可执行的命令,不过只有在 /etc/sudoers 配置文件中添加的用户才能使用该指令。

View File

@ -1062,20 +1062,21 @@ public void reOrderArray(int[] nums) {
<div align="center"> <img src="../pics//207c1801-2335-4b1b-b65c-126a0ba966cb.png" width="500"/> </div><br> <div align="center"> <img src="../pics//207c1801-2335-4b1b-b65c-126a0ba966cb.png" width="500"/> </div><br>
```java ```java
public ListNode FindKthToTail(ListNode head, int k) { public ListNode FindKthToTail(ListNode head, int k)
{
if (head == null) if (head == null)
return null; return null;
ListNode fast, slow; ListNode P1 = head;
fast = slow = head; while (P1 != null && k-- > 0)
while (fast != null && k-- > 0) P1 = P1.next;
fast = fast.next;
if (k > 0) if (k > 0)
return null; return null;
while (fast != null) { ListNode P2 = head;
fast = fast.next; while (P1 != null) {
slow = slow.next; P1 = P1.next;
P2 = P2.next;
} }
return slow; return P2;
} }
``` ```
@ -1083,6 +1084,12 @@ public ListNode FindKthToTail(ListNode head, int k) {
[NowCoder](https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4?tpId=13&tqId=11208&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking) [NowCoder](https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4?tpId=13&tqId=11208&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
## 题目描述
一个链表中包含环,请找出该链表的环的入口结点。
要求不能使用额外的空间。
## 解题思路 ## 解题思路
使用双指针,一个指针 fast 每次移动两个节点,一个指针 slow 每次移动一个节点。因为存在环,所以两个指针必定相遇在环中的某个节点上。假设相遇点在下图的 z1 位置,此时 fast 移动的节点数为 x+2y+zslow 为 x+y由于 fast 速度比 slow 快一倍,因此 x+2y+z=2(x+y),得到 x=z。 使用双指针,一个指针 fast 每次移动两个节点,一个指针 slow 每次移动一个节点。因为存在环,所以两个指针必定相遇在环中的某个节点上。假设相遇点在下图的 z1 位置,此时 fast 移动的节点数为 x+2y+zslow 为 x+y由于 fast 速度比 slow 快一倍,因此 x+2y+z=2(x+y),得到 x=z。
@ -1092,16 +1099,15 @@ public ListNode FindKthToTail(ListNode head, int k) {
<div align="center"> <img src="../pics//71363383-2d06-4c63-8b72-c01c2186707d.png" width="600"/> </div><br> <div align="center"> <img src="../pics//71363383-2d06-4c63-8b72-c01c2186707d.png" width="600"/> </div><br>
```java ```java
public ListNode EntryNodeOfLoop(ListNode pHead) { public ListNode EntryNodeOfLoop(ListNode pHead)
{
if (pHead == null || pHead.next == null) if (pHead == null || pHead.next == null)
return null; return null;
ListNode slow = pHead, fast = pHead; ListNode slow = pHead, fast = pHead;
while (fast != null && fast.next != null) { do {
fast = fast.next.next; fast = fast.next.next;
slow = slow.next; slow = slow.next;
if (slow == fast) } while (slow != fast);
break;
}
fast = pHead; fast = pHead;
while (slow != fast) { while (slow != fast) {
slow = slow.next; slow = slow.next;
@ -1159,7 +1165,8 @@ public ListNode ReverseList(ListNode head) {
### 递归 ### 递归
```java ```java
public ListNode Merge(ListNode list1, ListNode list2) { public ListNode Merge(ListNode list1, ListNode list2)
{
if (list1 == null) if (list1 == null)
return list2; return list2;
if (list2 == null) if (list2 == null)
@ -1177,7 +1184,8 @@ public ListNode Merge(ListNode list1, ListNode list2) {
### 迭代 ### 迭代
```java ```java
public ListNode Merge(ListNode list1, ListNode list2) { public ListNode Merge(ListNode list1, ListNode list2)
{
ListNode head = new ListNode(-1); ListNode head = new ListNode(-1);
ListNode cur = head; ListNode cur = head;
while (list1 != null && list2 != null) { while (list1 != null && list2 != null) {
@ -1209,20 +1217,22 @@ public ListNode Merge(ListNode list1, ListNode list2) {
## 解题思路 ## 解题思路
```java ```java
public boolean HasSubtree(TreeNode root1, TreeNode root2) { public boolean HasSubtree(TreeNode root1, TreeNode root2)
{
if (root1 == null || root2 == null) if (root1 == null || root2 == null)
return false; return false;
return isSubtree(root1, root2) || HasSubtree(root1.left, root2) || HasSubtree(root1.right, root2); return isSubtreeWithRoot(root1, root2) || HasSubtree(root1.left, root2) || HasSubtree(root1.right, root2);
} }
private boolean isSubtree(TreeNode root1, TreeNode root2) { private boolean isSubtreeWithRoot(TreeNode root1, TreeNode root2)
{
if (root2 == null) if (root2 == null)
return true; return true;
if (root1 == null) if (root1 == null)
return false; return false;
if (root1.val != root2.val) if (root1.val != root2.val)
return false; return false;
return isSubtree(root1.left, root2.left) && isSubtree(root1.right, root2.right); return isSubtreeWithRoot(root1.left, root2.left) && isSubtreeWithRoot(root1.right, root2.right);
} }
``` ```
@ -1237,7 +1247,8 @@ private boolean isSubtree(TreeNode root1, TreeNode root2) {
## 解题思路 ## 解题思路
```java ```java
public void Mirror(TreeNode root) { public void Mirror(TreeNode root)
{
if (root == null) if (root == null)
return; return;
swap(root); swap(root);
@ -1245,7 +1256,8 @@ public void Mirror(TreeNode root) {
Mirror(root.right); Mirror(root.right);
} }
private void swap(TreeNode root) { private void swap(TreeNode root)
{
TreeNode t = root.left; TreeNode t = root.left;
root.left = root.right; root.left = root.right;
root.right = t; root.right = t;
@ -1263,13 +1275,15 @@ private void swap(TreeNode root) {
## 解题思路 ## 解题思路
```java ```java
boolean isSymmetrical(TreeNode pRoot) { boolean isSymmetrical(TreeNode pRoot)
{
if (pRoot == null) if (pRoot == null)
return true; return true;
return isSymmetrical(pRoot.left, pRoot.right); return isSymmetrical(pRoot.left, pRoot.right);
} }
boolean isSymmetrical(TreeNode t1, TreeNode t2) { boolean isSymmetrical(TreeNode t1, TreeNode t2)
{
if (t1 == null && t2 == null) if (t1 == null && t2 == null)
return true; return true;
if (t1 == null || t2 == null) if (t1 == null || t2 == null)
@ -1293,7 +1307,8 @@ boolean isSymmetrical(TreeNode t1, TreeNode t2) {
## 解题思路 ## 解题思路
```java ```java
public ArrayList<Integer> printMatrix(int[][] matrix) { public ArrayList<Integer> printMatrix(int[][] matrix)
{
ArrayList<Integer> ret = new ArrayList<>(); ArrayList<Integer> ret = new ArrayList<>();
int r1 = 0, r2 = matrix.length - 1, c1 = 0, c2 = matrix[0].length - 1; int r1 = 0, r2 = matrix.length - 1, c1 = 0, c2 = matrix[0].length - 1;
while (r1 <= r2 && c1 <= c2) { while (r1 <= r2 && c1 <= c2) {
@ -1324,24 +1339,28 @@ public ArrayList<Integer> printMatrix(int[][] matrix) {
## 解题思路 ## 解题思路
```java ```java
private Stack<Integer> stack = new Stack<>(); private Stack<Integer> dataStack = new Stack<>();
private Stack<Integer> minStack = new Stack<>(); private Stack<Integer> minStack = new Stack<>();
public void push(int node) { public void push(int node)
stack.push(node); {
dataStack.push(node);
minStack.push(minStack.isEmpty() ? node : Math.min(minStack.peek(), node)); minStack.push(minStack.isEmpty() ? node : Math.min(minStack.peek(), node));
} }
public void pop() { public void pop()
stack.pop(); {
dataStack.pop();
minStack.pop(); minStack.pop();
} }
public int top() { public int top()
return stack.peek(); {
return dataStack.peek();
} }
public int min() { public int min()
{
return minStack.peek(); return minStack.peek();
} }
``` ```
@ -1359,12 +1378,13 @@ public int min() {
使用一个栈来模拟压入弹出操作。 使用一个栈来模拟压入弹出操作。
```java ```java
public boolean IsPopOrder(int[] pushA, int[] popA) { public boolean IsPopOrder(int[] pushSequence, int[] popSequence)
int n = pushA.length; {
int n = pushSequence.length;
Stack<Integer> stack = new Stack<>(); Stack<Integer> stack = new Stack<>();
for (int pushIndex = 0, popIndex = 0; pushIndex < n; pushIndex++) { for (int pushIndex = 0, popIndex = 0; pushIndex < n; pushIndex++) {
stack.push(pushA[pushIndex]); stack.push(pushSequence[pushIndex]);
while (popIndex < n && stack.peek() == popA[popIndex]) { while (popIndex < n && stack.peek() == popSequence[popIndex]) {
stack.pop(); stack.pop();
popIndex++; popIndex++;
} }
@ -1392,21 +1412,20 @@ public boolean IsPopOrder(int[] pushA, int[] popA) {
不需要使用两个队列分别存储当前层的节点和下一层的节点,因为在开始遍历一层的节点时,当前队列中的节点数就是当前层的节点数,只要控制遍历这么多节点数,就能保证这次遍历的都是当前层的节点。 不需要使用两个队列分别存储当前层的节点和下一层的节点,因为在开始遍历一层的节点时,当前队列中的节点数就是当前层的节点数,只要控制遍历这么多节点数,就能保证这次遍历的都是当前层的节点。
```java ```java
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { public ArrayList<Integer> PrintFromTopToBottom(TreeNode root)
{
Queue<TreeNode> queue = new LinkedList<>(); Queue<TreeNode> queue = new LinkedList<>();
ArrayList<Integer> ret = new ArrayList<>(); ArrayList<Integer> ret = new ArrayList<>();
if (root == null)
return ret;
queue.add(root); queue.add(root);
while (!queue.isEmpty()) { while (!queue.isEmpty()) {
int cnt = queue.size(); int cnt = queue.size();
while (cnt-- > 0) { while (cnt-- > 0) {
TreeNode t = queue.poll(); TreeNode t = queue.poll();
if (t.left != null) if (t == null)
queue.add(t.left); continue;
if (t.right != null)
queue.add(t.right);
ret.add(t.val); ret.add(t.val);
queue.add(t.left);
queue.add(t.right);
} }
} }
return ret; return ret;
@ -1424,10 +1443,9 @@ public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
## 解题思路 ## 解题思路
```java ```java
ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) { ArrayList<ArrayList<Integer>> Print(TreeNode pRoot)
{
ArrayList<ArrayList<Integer>> ret = new ArrayList<>(); ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
if (pRoot == null)
return ret;
Queue<TreeNode> queue = new LinkedList<>(); Queue<TreeNode> queue = new LinkedList<>();
queue.add(pRoot); queue.add(pRoot);
while (!queue.isEmpty()) { while (!queue.isEmpty()) {
@ -1435,13 +1453,14 @@ ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
int cnt = queue.size(); int cnt = queue.size();
while (cnt-- > 0) { while (cnt-- > 0) {
TreeNode node = queue.poll(); TreeNode node = queue.poll();
if (node == null)
continue;
list.add(node.val); list.add(node.val);
if (node.left != null) queue.add(node.left);
queue.add(node.left); queue.add(node.right);
if (node.right != null)
queue.add(node.right);
} }
ret.add(list); if (list.size() != 0)
ret.add(list);
} }
return ret; return ret;
} }
@ -1458,10 +1477,9 @@ ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
## 解题思路 ## 解题思路
```java ```java
public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) { public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot)
{
ArrayList<ArrayList<Integer>> ret = new ArrayList<>(); ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
if (pRoot == null)
return ret;
Queue<TreeNode> queue = new LinkedList<>(); Queue<TreeNode> queue = new LinkedList<>();
queue.add(pRoot); queue.add(pRoot);
boolean reverse = false; boolean reverse = false;
@ -1470,16 +1488,17 @@ public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
int cnt = queue.size(); int cnt = queue.size();
while (cnt-- > 0) { while (cnt-- > 0) {
TreeNode node = queue.poll(); TreeNode node = queue.poll();
if (node == null)
continue;
list.add(node.val); list.add(node.val);
if (node.left != null) queue.add(node.left);
queue.add(node.left); queue.add(node.right);
if (node.right != null)
queue.add(node.right);
} }
if (reverse) if (reverse)
Collections.reverse(list); Collections.reverse(list);
reverse = !reverse; reverse = !reverse;
ret.add(list); if (list.size() != 0)
ret.add(list);
} }
return ret; return ret;
} }
@ -1500,13 +1519,15 @@ public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
## 解题思路 ## 解题思路
```java ```java
public boolean VerifySquenceOfBST(int[] sequence) { public boolean VerifySquenceOfBST(int[] sequence)
{
if (sequence == null || sequence.length == 0) if (sequence == null || sequence.length == 0)
return false; return false;
return verify(sequence, 0, sequence.length - 1); return verify(sequence, 0, sequence.length - 1);
} }
private boolean verify(int[] sequence, int first, int last) { private boolean verify(int[] sequence, int first, int last)
{
if (last - first <= 1) if (last - first <= 1)
return true; return true;
int rootVal = sequence[last]; int rootVal = sequence[last];
@ -1537,18 +1558,20 @@ private boolean verify(int[] sequence, int first, int last) {
```java ```java
private ArrayList<ArrayList<Integer>> ret = new ArrayList<>(); private ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int target) { public ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int target)
{
backtracking(root, target, new ArrayList<>()); backtracking(root, target, new ArrayList<>());
return ret; return ret;
} }
private void backtracking(TreeNode node, int target, ArrayList<Integer> path) { private void backtracking(TreeNode node, int target, ArrayList<Integer> path)
{
if (node == null) if (node == null)
return; return;
path.add(node.val); path.add(node.val);
target -= node.val; target -= node.val;
if (target == 0 && node.left == null && node.right == null) { if (target == 0 && node.left == null && node.right == null) {
ret.add(new ArrayList(path)); ret.add(new ArrayList<>(path));
} else { } else {
backtracking(node.left, target, path); backtracking(node.left, target, path);
backtracking(node.right, target, path); backtracking(node.right, target, path);
@ -1582,7 +1605,8 @@ private void backtracking(TreeNode node, int target, ArrayList<Integer> path) {
<div align="center"> <img src="../pics//8f3b9519-d705-48fe-87ad-2e4052fc81d2.png" width="600"/> </div><br> <div align="center"> <img src="../pics//8f3b9519-d705-48fe-87ad-2e4052fc81d2.png" width="600"/> </div><br>
```java ```java
public RandomListNode Clone(RandomListNode pHead) { public RandomListNode Clone(RandomListNode pHead)
{
if (pHead == null) if (pHead == null)
return null; return null;
// 插入新节点 // 插入新节点
@ -1629,14 +1653,14 @@ public RandomListNode Clone(RandomListNode pHead) {
private TreeNode pre = null; private TreeNode pre = null;
private TreeNode head = null; private TreeNode head = null;
public TreeNode Convert(TreeNode root) { public TreeNode Convert(TreeNode root)
if (root == null) {
return null;
inOrder(root); inOrder(root);
return head; return head;
} }
private void inOrder(TreeNode node) { private void inOrder(TreeNode node)
{
if (node == null) if (node == null)
return; return;
inOrder(node.left); inOrder(node.left);
@ -1661,35 +1685,35 @@ private void inOrder(TreeNode node) {
## 解题思路 ## 解题思路
```java ```java
public class Solution { private String deserializeStr;
private String deserializeStr; public String Serialize(TreeNode root)
{
if (root == null)
return "#";
return root.val + " " + Serialize(root.left) + " " + Serialize(root.right);
}
public String Serialize(TreeNode root) { public TreeNode Deserialize(String str)
if (root == null) {
return "#"; deserializeStr = str;
return root.val + " " + Serialize(root.left) + " " + Serialize(root.right); return Deserialize();
} }
public TreeNode Deserialize(String str) { private TreeNode Deserialize()
deserializeStr = str; {
return Deserialize(); if (deserializeStr.length() == 0)
} return null;
int index = deserializeStr.indexOf(" ");
private TreeNode Deserialize() { String node = index == -1 ? deserializeStr : deserializeStr.substring(0, index);
if (deserializeStr.length() == 0) deserializeStr = index == -1 ? "" : deserializeStr.substring(index + 1);
return null; if (node.equals("#"))
int index = deserializeStr.indexOf(" "); return null;
String node = index == -1 ? deserializeStr : deserializeStr.substring(0, index); int val = Integer.valueOf(node);
deserializeStr = index == -1 ? "" : deserializeStr.substring(index + 1); TreeNode t = new TreeNode(val);
if (node.equals("#")) t.left = Deserialize();
return null; t.right = Deserialize();
int val = Integer.valueOf(node); return t;
TreeNode t = new TreeNode(val);
t.left = Deserialize();
t.right = Deserialize();
return t;
}
} }
``` ```