Skip to content

Commit

Permalink
Merge pull request 0voice#7 from 0voice/master
Browse files Browse the repository at this point in the history
feat(All):update all
  • Loading branch information
EvanLeung08 authored Dec 1, 2019
2 parents d86ac50 + 926b98e commit 9f4ed67
Show file tree
Hide file tree
Showing 23 changed files with 636 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,62 @@ typedef struct node{
void reverse(node* head)
{
if(NULL == head || NULL == head->next){
if(head == NULL){
return;
}
node* prev=NULL;
node* pcur=head->next;
node* next;
while(pcur!=NULL){
if(pcur->next==NULL){
pcur->next=prev;
break;
}
next=pcur->next;
pcur->next=prev;
prev=pcur;
pcur=next;
node* pleft = NULL;
node* pcurrent = head;
node* pright = head->next;
while(pright){
pcurrent->next = pleft;
node *ptemp = pright->next;
pright->next = pcurrent;
pleft = pcurrent;
pcurrent = pright;
pright = ptemp;
}
head->next=pcur;
node*tmp=head->next;
while(tmp!=NULL){
cout<<tmp->data<<"\t";
tmp=tmp->next;
while(pcurrent != NULL){
cout<< pcurrent->data << "\t";
pcurrent = pcurrent->next;
}
}
```

``` java
class Solution<T> {

public void reverse(ListNode<T> head) {
if (head == null || head.next == null) {
return ;
}
ListNode<T> currentNode = head;
Stack<ListNode<T>> stack = new Stack<>();
while (currentNode != null) {
stack.push(currentNode);
ListNode<T> tempNode = currentNode.next;
currentNode.next = null; // 断开连接
currentNode = tempNode;
}

head = stack.pop();
currentNode = head;

while (!stack.isEmpty()) {
currentNode.next = stack.pop();
currentNode = currentNode.next;
}
}
}

class ListNode<T>{
T val;
public ListNode(T val) {
this.val = val;
}
ListNode<T> next;
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
```
说明:保证输入的 K 满足 1<=K<=(节点数目)

树相关的题目,第一眼就想到递归求解,左右子树分别遍历。联想到二叉搜索树的性质,root 大于左子树,小于右子树,如果左子树的节点数目等于 K-1,那么 root 就是结果,否则如果左子树节点数目小于 K-1,那么结果必然在右子树,否则就在左子树。因此在搜索的时候同时返回节点数目,跟 K 做对比,就能得出结果了。
解法1:树相关的题目,第一眼就想到递归求解,左右子树分别遍历。联想到二叉搜索树的性质,root 大于左子树,小于右子树,如果左子树的节点数目等于 K-1,那么 root 就是结果,否则如果左子树节点数目小于 K-1,那么结果必然在右子树,否则就在左子树。因此在搜索的时候同时返回节点数目,跟 K 做对比,就能得出结果了。

```
/**
Expand Down Expand Up @@ -79,3 +79,260 @@ class Solution {
```

解法2:基于二叉搜索树的特性,在中序遍历的结果中,第k个元素就是本题的解。
最差的情况是k节点是bst的最右叶子节点,不过`每个节点的遍历次数最多是1次`
遍历并不是需要全部做完,使用计数的方式,找到第k个元素就可以退出。
下面是go的一个简单实现。

```
// BST is binary search tree
type BST struct {
key, value int
left, right *BST
}
func (bst *BST) setLeft(b *BST) {
bst.left = b
}
func (bst *BST) setRight(b *BST) {
bst.right = b
}
// count 查找bst第k个节点的值,未找到就返回0
func count(bst *BST, k int) int {
if k < 1 {
return 0
}
c := 0
ok, value := countRecursive(bst, &c, k)
if ok {
return value
}
return 0
}
// countRecurisive 对bst使用中序遍历
// 用计数方式控制退出遍历,参数c就是已遍历节点数
func countRecursive(bst *BST, c *int, k int) (bool, int) {
if bst.left != nil {
ok, value := countRecursive(bst.left, c, k)
if ok {
return ok, value
}
}
if *c == k-1 {
return true, bst.value
}
*c++
if bst.right != nil {
ok, value := countRecursive(bst.right, c, k)
if ok {
return ok, value
}
}
return false, 0
}
// 下面是测试代码,覆盖了退化的情况和普通bst
func createBST1() *BST {
b1 := &BST{key: 1, value: 10}
b2 := &BST{key: 2, value: 20}
b3 := &BST{key: 3, value: 30}
b4 := &BST{key: 4, value: 40}
b5 := &BST{key: 5, value: 50}
b6 := &BST{key: 6, value: 60}
b7 := &BST{key: 7, value: 70}
b8 := &BST{key: 8, value: 80}
b9 := &BST{key: 9, value: 90}
b9.setLeft(b8)
b8.setLeft(b7)
b7.setLeft(b6)
b6.setLeft(b5)
b5.setLeft(b4)
b4.setLeft(b3)
b3.setLeft(b2)
b2.setLeft(b1)
return b9
}
func createBST2() *BST {
b1 := &BST{key: 1, value: 10}
b2 := &BST{key: 2, value: 20}
b3 := &BST{key: 3, value: 30}
b4 := &BST{key: 4, value: 40}
b5 := &BST{key: 5, value: 50}
b6 := &BST{key: 6, value: 60}
b7 := &BST{key: 7, value: 70}
b8 := &BST{key: 8, value: 80}
b9 := &BST{key: 9, value: 90}
b1.setRight(b2)
b2.setRight(b3)
b3.setRight(b4)
b4.setRight(b5)
b5.setRight(b6)
b6.setRight(b7)
b7.setRight(b8)
b8.setRight(b9)
return b1
}
func createBST3() *BST {
b1 := &BST{key: 1, value: 10}
b2 := &BST{key: 2, value: 20}
b3 := &BST{key: 3, value: 30}
b4 := &BST{key: 4, value: 40}
b5 := &BST{key: 5, value: 50}
b6 := &BST{key: 6, value: 60}
b7 := &BST{key: 7, value: 70}
b8 := &BST{key: 8, value: 80}
b9 := &BST{key: 9, value: 90}
b5.setLeft(b3)
b5.setRight(b7)
b3.setLeft(b2)
b3.setRight(b4)
b2.setLeft(b1)
b7.setLeft(b6)
b7.setRight(b8)
b8.setRight(b9)
return b5
}
func createBST4() *BST {
b := &BST{key: 1, value: 10}
last := b
for i := 2; i < 100000; i++ {
n := &BST{key: i, value: i * 10}
last.setRight(n)
last = n
}
return b
}
func createBST5() *BST {
b := &BST{key: 99999, value: 999990}
last := b
for i := 99998; i > 0; i-- {
n := &BST{key: i, value: i * 10}
last.setLeft(n)
last = n
}
return b
}
func createBST6() *BST {
b := &BST{key: 50000, value: 500000}
last := b
for i := 49999; i > 0; i-- {
n := &BST{key: i, value: i * 10}
last.setLeft(n)
last = n
}
last = b
for i := 50001; i < 100000; i++ {
n := &BST{key: i, value: i * 10}
last.setRight(n)
last = n
}
return b
}
func TestK(t *testing.T) {
bst1 := createBST1()
bst2 := createBST2()
bst3 := createBST3()
bst4 := createBST4()
check(t, bst1, 1, 10)
check(t, bst1, 2, 20)
check(t, bst1, 3, 30)
check(t, bst1, 4, 40)
check(t, bst1, 5, 50)
check(t, bst1, 6, 60)
check(t, bst1, 7, 70)
check(t, bst1, 8, 80)
check(t, bst1, 9, 90)
check(t, bst2, 1, 10)
check(t, bst2, 2, 20)
check(t, bst2, 3, 30)
check(t, bst2, 4, 40)
check(t, bst2, 5, 50)
check(t, bst2, 6, 60)
check(t, bst2, 7, 70)
check(t, bst2, 8, 80)
check(t, bst2, 9, 90)
check(t, bst3, 1, 10)
check(t, bst3, 2, 20)
check(t, bst3, 3, 30)
check(t, bst3, 4, 40)
check(t, bst3, 5, 50)
check(t, bst3, 6, 60)
check(t, bst3, 7, 70)
check(t, bst3, 8, 80)
check(t, bst3, 9, 90)
check(t, bst4, 1, 10)
check(t, bst4, 2, 20)
check(t, bst4, 3, 30)
check(t, bst4, 4, 40)
check(t, bst4, 5, 50)
check(t, bst4, 6, 60)
check(t, bst4, 7, 70)
check(t, bst4, 8, 80)
check(t, bst4, 9, 90)
check(t, bst4, 99991, 999910)
check(t, bst4, 99992, 999920)
check(t, bst4, 99993, 999930)
check(t, bst4, 99994, 999940)
check(t, bst4, 99995, 999950)
check(t, bst4, 99996, 999960)
check(t, bst4, 99997, 999970)
check(t, bst4, 99998, 999980)
check(t, bst4, 99999, 999990)
}
func check(t *testing.T, b *BST, k, value int) {
t.Helper()
checkCall(t, b, k, value, count)
// 此处可添加其他解法的实现
}
func checkCall(t *testing.T, b *BST, k, value int, find func(bst *BST, kth int) int) {
t.Helper()
got := find(b, k)
if got != value {
t.Fatalf("want:%d, got:%d", value, got)
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
#### **出题人**:阿里巴巴出题专家:怀虎/阿里云云效平台负责人

#### **参考答案**
首先根据目的IP和路由表决定走哪个网卡,再根据网卡的子网掩码地址判断目的IP是否在子网内。如果不在则会通过arp缓存查询IP的网卡地址,不存在的话会通过广播询问目的IP的mac地址,得到后就开始发包了,同时mac地址也会被arp缓存起来。
ping目标ip时,先查路由表,确定出接口
- 如果落在直连接口子网内,此时若为以太网等 _多路访问网络_ 则先查询arp缓存,命中则直接发出,否则在该接口上发arp询问目标ip的mac地址,取得后发出,若为ppp等 _点对点网络_ ,则直接可以发出;
- 如果查表落在缺省路由上,此时若为以太网等 _多路访问网络_ 则先查询网关arp缓存,命中则直接发出,否则在该接口上发arp询问网关的mac地址,取得后发出,若为ppp等 _点对点网络_ ,则直接可以发出;
- 若查表未命中,则返回不可达。
Loading

0 comments on commit 9f4ed67

Please sign in to comment.