LeetCode進階339-深度優先搜索(DFS)

概念

深度優先搜索算法(英語:Depth-First-Search,DFS)是一種用於遍歷或搜索樹或圖的算法。沿着樹的深度遍歷樹的節點,儘量深的搜索樹的分支。當節點v的所在邊都己被探尋過,搜索將回溯到發現節點v的那條邊的起始節點。這一過程一直進行到已發現從源節點可達的全部節點爲止。若是還存在未被發現的節點,則選擇其中一個做爲源節點並重復以上過程,整個進程反覆進行直到全部節點都被訪問爲止。屬於盲目搜索。算法

深度優先搜索是圖論中的經典算法,利用深度優先搜索算法能夠產生目標圖的相應拓撲排序表,利用拓撲排序表能夠方便的解決不少相關的圖論問題,如最大路徑問題等等。bash

因發明「深度優先搜索算法」,約翰·霍普克洛夫特與羅伯特·塔揚在1986年共同得到計算機領域的最高獎:圖靈獎。(摘自-維基百科)數據結構

原題

339. Nested List Weight Sum

Given a nested list of integers, return the sum of all integers in the list weighted by their depth.ui

Each element is either an integer, or a list -- whose elements may also be integers or other lists.this

Example 1:編碼

Input: [[1,1],2,[1,1]] Output: 10 Explanation: Four 1's at depth 2, one 2 at depth 1. Example 2:spa

Input: [1,[4,[6]]] Output: 27 Explanation: One 1 at depth 1, one 4 at depth 2, and one 6 at depth 3; 1 + 42 + 63 = 27.設計

339. 嵌套列表權重求和

給定一個嵌套整數列表,返回按照深度優先搜索得出的全部元素的權重和3d

每一個元素都是整數或者列表——列表的元素也一樣是整數或列表code

例1:

輸入:[[1,1],2,[1,1]] 輸出:10 說明:4個「1」深度爲2,一個2深度爲1.

例2:

輸入:[1,[4,[6]]] 輸出:27 說明:1個「1」深度爲1,一個4深度爲2,一個6深度爲3;1 + 42 +63 = 27.

  • 本題在LeetCode上屬於深度優先搜多算法分類下

原題提示

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * public interface NestedInteger {
 *     // Constructor initializes an empty nested list.
 *     public NestedInteger();
 *
 *     // Constructor initializes a single integer.
 *     public NestedInteger(int value);
 *
 *     // @return true if this NestedInteger holds a single integer, rather than a nested list.
 *     public boolean isInteger();
 *
 *     // @return the single integer that this NestedInteger holds, if it holds a single integer
 *     // Return null if this NestedInteger holds a nested list
 *     public Integer getInteger();
 *
 *     // Set this NestedInteger to hold a single integer.
 *     public void setInteger(int value);
 *
 *     // Set this NestedInteger to hold a nested list and adds a nested integer to it.
 *     public void add(NestedInteger ni);
 *
 *     // @return the nested list that this NestedInteger holds, if it holds a nested list
 *     // Return null if this NestedInteger holds a single integer
 *     public List<NestedInteger> getList();
 * }
 */
複製代碼

題意分析

根據題目提示的數據結構,觀察NestedInteger的方法,isInteger()表示當前元素是否包含Integer數據,true表明是,false表明嵌套包含列表。getInteger()表示獲取當前元素包含的Integer數值。根據題意,因爲嵌套存儲的數據結構,不難聯想到典型的深度優先搜索思路

思路設計

對當前列表搜索時,若當前元素僅包含Integer數而非嵌套結構則計算當前節點元素(深度*Integer)值得出當前元素的權重再進行累加;若當前元素爲嵌套的List結構可遞歸計算當前子列表的權重和

僞代碼:

1、聲明dfs方法,dfs方法有兩個參數,list列表以及節點深度;
     一、聲明int類型權重和變量sum=0;
     二、循環遍歷輸入參數List<NestedInteger>列表;
       i.若列表元素爲Integer非列表(嵌套子列表),計算當前元素的權重爲Integer值*深度;將計算獲得的權重值累加到總權重和變量sum中;
       ii.若列表元素非Integer而是嵌套子列表,則遞歸調用dfs方法計算列表的權重和,而此時元素深度須要加1;
     三、循環結束返回權重和sum;  
     
   2、最外層元素節點深度爲1,直接調用dfs方法傳入列表list,以及深度1直接獲得權重總和;
     
複製代碼

編碼實踐

private int dfs(List<NestedInteger> list, int depth) {
		int sum = 0;
		for (NestedInteger e : list) {
			sum += e.isInteger() ? e.getInteger() * depth : helper(e.getList(), depth + 1);
		}
		return sum;
	}
	
	public int depthSum(List<NestedInteger> nestedList) {
		return helper(nestedList, 1);
	}	

複製代碼

結語

本篇題目相對簡單,重點是理解深度優先遍歷思想和簡單實踐,無彩蛋。關於深度優先算法,後續還有進階博客題解說明,敬請期待~

關注訂閱號 獲取更多幹貨~
相關文章
相關標籤/搜索