<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)582:殺掉進(jìn)程

          共 2824字,需瀏覽 6分鐘

           ·

          2022-04-17 23:18

          算法的重要性,我就不多說(shuō)了吧,想去大廠,就必須要經(jīng)過(guò)基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問(wèn)題叫做?殺掉進(jìn)程,我們先來(lái)看題面:
          https://leetcode-cn.com/problems/kill-process/


          Given?n?processes, each process has a unique?PID (process id)?and its?PPID (parent process id).
          Each process only has one parent process, but may have one or more children processes. This is just like a tree structure. Only one process has PPID that is 0, which means this process has no parent process. All the PIDs will be distinct positive integers.
          We use two list of integers to represent a list of processes, where the first list contains PID for each process and the second list contains the corresponding PPID.
          Now given the two lists, and a PID representing a process you want to kill, return a list of PIDs of processes that will be killed in the end. You should assume that when a process is killed, all its children processes will be killed. No order is required for the final answer.


          給 n 個(gè)進(jìn)程,每個(gè)進(jìn)程都有一個(gè)獨(dú)一無(wú)二的 PID (進(jìn)程編號(hào))和它的 PPID (父進(jìn)程編號(hào))。

          每一個(gè)進(jìn)程只有一個(gè)父進(jìn)程,但是每個(gè)進(jìn)程可能會(huì)有一個(gè)或者多個(gè)孩子進(jìn)程。它們形成的關(guān)系就像一個(gè)樹(shù)狀結(jié)構(gòu)。只有一個(gè)進(jìn)程的 PPID 是 0 ,意味著這個(gè)進(jìn)程沒(méi)有父進(jìn)程。所有的 PID 都會(huì)是唯一的正整數(shù)。

          我們用兩個(gè)序列來(lái)表示這些進(jìn)程,第一個(gè)序列包含所有進(jìn)程的 PID ,第二個(gè)序列包含所有進(jìn)程對(duì)應(yīng)的 PPID。

          現(xiàn)在給定這兩個(gè)序列和一個(gè) PID 表示你要?dú)⑺赖倪M(jìn)程,函數(shù)返回一個(gè) PID 序列,表示因?yàn)闅⑦@個(gè)進(jìn)程而導(dǎo)致的所有被殺掉的進(jìn)程的編號(hào)。

          當(dāng)一個(gè)進(jìn)程被殺掉的時(shí)候,它所有的孩子進(jìn)程和后代進(jìn)程都要被殺掉。

          你可以以任意順序排列返回的 PID 序列。

          示例


          解題


          https://blog.csdn.net/qq_29051413/article/details/108530711


          主要思路:

          ?先把所有進(jìn)程的子進(jìn)程統(tǒng)計(jì)好存入一個(gè) HashMap 中,key 為進(jìn)程編號(hào)
          value 為子進(jìn)程編號(hào)集合。
          當(dāng)要?dú)⑺谰幪?hào)為 kill 的進(jìn)程時(shí),從 map 中找到 kill 的子進(jìn)程,添加到待殺死 list 中,接著深度優(yōu)先搜索 kill 子進(jìn)程的子進(jìn)程,一律裝入到 list 中。
          返回 list。

          class?Solution?{
          ????/**
          ?????* 當(dāng)一個(gè)進(jìn)程被殺掉的時(shí)候,它所有的孩子進(jìn)程和后代進(jìn)程都要被殺掉。可以以任意順序排列返回的 PID 序列。
          ?????* pid[i] 的父進(jìn)程為 ppid[i]
          ?????*
          ?????* @param?pid 進(jìn)程編號(hào),只有一個(gè)進(jìn)程的 PPID 是 0 ,意味著這個(gè)進(jìn)程沒(méi)有父進(jìn)程
          ?????* @param?ppid 父進(jìn)程編號(hào)
          ?????* @param?kill 待殺死的進(jìn)程編號(hào)
          ?????* @return?被殺死的進(jìn)程的編號(hào)集合
          ?????*/

          ????public?List killProcess(List pid, List ppid, int kill) {
          ????????// key 為父進(jìn)程編號(hào),value 為該進(jìn)程的子進(jìn)程編號(hào)集合
          ????????HashMapList> map = new?HashMap<>();
          ????????// 遍歷父進(jìn)程編號(hào)集合
          ????????for?(int i = 0; i < ppid.size(); i++) {
          ????????????if?(ppid.get(i) > 0) {
          ????????????????List l = map.getOrDefault(ppid.get(i), new?ArrayList()); // 取出子進(jìn)程集合
          ????????????????l.add(pid.get(i)); // 添加新的子進(jìn)程編號(hào)
          ????????????????map.put(ppid.get(i), l); // 把子進(jìn)程集合放回去
          ????????????}
          ????????}
          ????????List l = new?ArrayList<>();
          ????????l.add(kill);
          ????????getAllChildren(map, l, kill);
          ????????return?l;
          ????}

          ????/**
          ?????* 獲取進(jìn)程 kill 的所有子孫進(jìn)程并裝入到 l 中
          ?????* @param?map
          ?????* @param?l
          ?????* @param?kill
          ?????*/

          ????public?void getAllChildren(HashMapList> map, List l, int kill) {
          ????????if?(map.containsKey(kill)) { // 如果存在被殺死的進(jìn)程
          ????????????for?(int id : map.get(kill)) { // 取出待殺死進(jìn)程的子進(jìn)程編號(hào)集合
          ????????????????l.add(id); // 自己也要被殺死
          ????????????????getAllChildren(map, l, id); // dfs,子進(jìn)程的子進(jìn)程也要?dú)⑺?/span>
          ????????????}
          ????????}
          ????}
          }


          好了,今天的文章就到這里,如果覺(jué)得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

          上期推文:
          LeetCode1-580題匯總,希望對(duì)你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)581:最短無(wú)序連續(xù)子數(shù)組

          瀏覽 19
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  操逼视频在线播放 | 成人电影久久 | 精品无码免费一区二区 | 欧美激情三级 | 91在线无码精品秘 入口九色1 |