leetcode5000 從始點到終點的所有路徑

2021-09-24 05:08:14 字數 1367 閱讀 5298

看到這題的時候,第一反應就是每個節點的下乙個節點的個數不確定,所以沒辦法用陣列來表示,所以選擇使用map

然後之前用過stack來表示路徑,所以初始**如下(執行時間59ms):

map> map = new hashmap<>();

public boolean leadstodestination(int n, int edges, int source, int destination) else

}stackstack = new stack<>();

return leadstodestination(source, stack, destination);

}private boolean leadstodestination(int source, stackstack, int destination) else

}for (integer i : list)

stack.add(i);

boolean la = leadstodestination(i, stack, destination);

if (!la) else

}return true;

}

看了第一名的**。如醍醐灌頂,不能用陣列,可以用鍊錶啊,可以用boolean值來表示路徑啊

新的**如下(9ms):

class node 

public node(int n)

}boolean used;

public boolean leadstodestination(int n, int edges, int source, int destination)

used = new boolean[n];

//將edges[i][1]放到edges[i][0]的next中,相當於陣列,比陣列好的是不需要擴容

for (int i = 0; i < edges.length; i++)

return leadstodestination(source, nodes, destination);

}private boolean leadstodestination(int sta, node nodes, int destination) else

}used[sta] = true;

node p = nodes[sta].next;

while (p != null)

if (!leadstodestination(p.val, nodes, destination))

p = p.next;

}used[sta] = false;

return true;

}

從0加到5000微信好友的實操方法

現有資源 第一步,公司通訊錄,某某活動或培訓的通訊錄,qq號,挨個加,至少資訊發到。缺點 每小時只能加5 8個,每天最多加50個人。更好的辦法是 讓別人加你 發簡訊也好,聚會時雷達也好,掃一掃也好。第二步,解幫qq號,換老婆,親戚,朋友的qq號,繼續加。缺點 每小時只能加5 8個,每天最多加50個人...

從leetcode學到更多

4sum class solution public list foursum int num,int target arraylist ans new arraylist if num.length 4 return ans arrays.sort num for int i 0 itarget ...

leetcode423 從英文中重建數字

給定乙個非空字串,其中包含字母順序打亂的英文單詞表示的數字0 9。按公升序輸出原始的數字。注意 示例 1 輸入 owoztneoer 輸出 012 zeroonetwo 示例 2 輸入 fviefuro 輸出 45 fourfive 先統計給定字串中每個字元的出現頻率,然後分別計算每個數字出現的次數...