劍指Offer之Java算法習(xí)題精講排列與N叉樹(shù)
題目一
?解法
class Solution { LinkedList<List<Integer>> ans = new LinkedList<List<Integer>>(); public List<List<Integer>> permute(int[] nums) { LinkedList<Integer> list = new LinkedList<Integer>(); boolean[] bo = new boolean[nums.length]; method(nums,bo,list); return ans; } public void method(int[] nums,boolean[] bo ,LinkedList<Integer> list){ if(list.size()==nums.length){ ans.add(new LinkedList(list)); return; } for(int i = 0;i<nums.length;i++){ if(bo[i]){ continue; } list.add(nums[i]); bo[i] = true; method(nums,bo,list); list.removeLast(); bo[i] = false; } } }
題目二
?解法
/* // Definition for a Node. class Node { public int val; public List<Node> children; public Node() {} public Node(int _val) { val = _val; } public Node(int _val, List<Node> _children) { val = _val; children = _children; } }; */ class Solution { public int maxDepth(Node root) { if(root==null){ return 0; } int maxChildDepth = 0; for(int i = 0;i<root.children.size();i++){ int childDepth = maxDepth(root.children.get(i)); maxChildDepth = Math.max(maxChildDepth, childDepth); } return maxChildDepth+1; } }
到此這篇關(guān)于劍指Offer之Java算法習(xí)題精講排列與N叉樹(shù)的文章就介紹到這了,更多相關(guān)Java N叉樹(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Fluent MyBatis實(shí)現(xiàn)動(dòng)態(tài)SQL
MyBatis 令人喜歡的一大特性就是動(dòng)態(tài) SQL。本文主要介紹了Fluent MyBatis實(shí)現(xiàn)動(dòng)態(tài)SQL,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Java中jdk1.8和jdk17相互切換實(shí)戰(zhàn)步驟
之前做Java項(xiàng)目時(shí)一直用的是jdk1.8,現(xiàn)在想下載另一個(gè)jdk版本17,并且在之后的使用中可以進(jìn)行相互切換,下面這篇文章主要給大家介紹了關(guān)于Java中jdk1.8和jdk17相互切換的相關(guān)資料,需要的朋友可以參考下2023-05-05SpringBoot + WebSocket 實(shí)現(xiàn)答題對(duì)戰(zhàn)匹配機(jī)制案例詳解
這篇文章主要介紹了SpringBoot + WebSocket 實(shí)現(xiàn)答題對(duì)戰(zhàn)匹配機(jī)制,分別為每個(gè)用戶擬定四種在線狀態(tài),通過(guò)流程圖給大家展示,需要的朋友可以參考下2021-05-05Spring?Boot?打包成Jar包運(yùn)行原理分析
這篇文章主要為大家介紹了Spring?Boot?打包成Jar包運(yùn)行的原理分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Mybatis之collection標(biāo)簽中javaType和ofType屬性的區(qū)別說(shuō)明
這篇文章主要介紹了Mybatis之collection標(biāo)簽中javaType和ofType屬性的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12