美文网首页
DFS--Clone Graph \Target Sum

DFS--Clone Graph \Target Sum

作者: miky_zheng | 来源:发表于2019-02-13 13:51 被阅读0次

    temp1 遍历

    优点:简单。
    缺点:如果递归的深度太高,您将遭受堆栈溢出。在这种情况下,您可能希望改用BFS,或者使用显式堆栈实现DFS。

    /*
     * Return true if there is a path from cur to target.
     */
    boolean DFS(Node cur, Node target, Set<Node> visited) {
        return true if cur is target;
        for (next : each neighbor of cur) {
            if (next is not in visited) {
                add next to visted;
                return true if DFS(next, target, visited) == true;
            }
        }
        return false;
    }
    

    temp2 推荐

    优点:查询的路径即堆栈的深度,容易计算空间复杂度

    /*
     * Return true if there is a path from cur to target.
     */
    boolean DFS(int root, int target) {
        Set<Node> visited;
        Stack<Node> stack;
        add root to stack;
        while (s is not empty) {
            Node cur = the top element in stack;
            remove the cur from the stack;
            return true if cur is target;
            for (Node next : the neighbors of cur) {
                if (next is not in visited) {
                    add next to visited;
                    add next to stack;
                }
            }
        }
        return false;
    }
    

    Clone Graph

    Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph contains a label (int) and a list (List[UndirectedGraphNode]) of its neighbors. There is an edge between the given node and each of the nodes in its neighbors.
    OJ's undirected graph serialization (so you can understand error output):
    Nodes are labeled uniquely.
    We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.

    As an example, consider the serialized graph {0,1,2#1,2#2,2}.
    The graph has a total of three nodes, and therefore contains three parts as separated by #.
    First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
    Second node is labeled as 1. Connect node 1 to node 2.
    Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

    Visually, the graph looks like the following:

       1
      / \
     /   \
    0 --- 2
         / \
         \_/
    

    Note: The information about the tree serialization is only meant so that you can understand error output if you get a wrong answer. You don't need to understand the serialization to solve the problem.

    解法1:递归

        public class Solution {
            private HashMap<Integer, UndirectedGraphNode> hm=new HashMap<Integer, UndirectedGraphNode>();
            public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
                 if(node==null)
                     return null;
                 if(hm.containsKey(node.label))
                     return hm.get(node.label);
                 UndirectedGraphNode clone=new UndirectedGraphNode(node.label);
                 hm.put(node.label,clone);
                 for (UndirectedGraphNode temp : node.neighbors) {
                     clone.neighbors.add(temp);
                }
                
                return clone;
                
            }
            
           class UndirectedGraphNode {
                int label;
                List<UndirectedGraphNode> neighbors;
                UndirectedGraphNode(int x) { 
                 label = x; 
                 neighbors = new ArrayList<UndirectedGraphNode>(); 
                }
            }
        }
    

    Target Sum

    You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.
    
    Find out how many ways to assign symbols to make sum of integers equal to target S.
    
    Example 1:
    Input: nums is [1, 1, 1, 1, 1], S is 3. 
    Output: 5
    Explanation: 
    
    -1+1+1+1+1 = 3
    +1-1+1+1+1 = 3
    +1+1-1+1+1 = 3
    +1+1+1-1+1 = 3
    +1+1+1+1-1 = 3
    
    There are 5 ways to assign symbols to make the sum of nums be target 3.
    Note:
    The length of the given array is positive and will not exceed 20.
    The sum of elements in the given array will not exceed 1000.
    Your output answer is guaranteed to be fitted in a 32-bit integer.
    
    

    解法1:
    原理

    sum(P) - sum(N) = target
    sum(P) + sum(N) + sum(P) - sum(N) = target + sum(P) + sum(N)
    2 * sum(P) = target + sum(nums)

    subSetSum见 https://www.jianshu.com/p/847fe1674bea

      public int findTargetSumWays(int[] nums, int s) {
            int sum = 0;
            for (int n : nums)
                sum += n;
            return sum < s || (s + sum) % 2 > 0 ? 0 : subsetSum(nums, (s + sum) >>> 1); 
        }   
    
        public int subsetSum(int[] nums, int s) {
            int[] dp = new int[s + 1]; 
            dp[0] = 1;
            for (int n : nums)
                for (int i = s; i >= n; i--)
                    dp[i] += dp[i - n]; 
            return dp[s];
        } 
    
    常规解法:(遍历)
        int result = 0;
        
        public int findTargetSumWays(int[] nums, int S) {
            if (nums == null || nums.length == 0) return result;
            helper(nums, S, 0, 0);
            return result;
        }
        
        public void helper(int[] nums, int target, int pos, long eval){
            if (pos == nums.length) {
                if (target == eval) result++;
                return;
            }
            helper(nums, target, pos + 1, eval + nums[pos]);
            helper(nums, target, pos + 1, eval - nums[pos]);
        }
    

    相关文章

      网友评论

          本文标题:DFS--Clone Graph \Target Sum

          本文链接:https://www.haomeiwen.com/subject/lltdeqtx.html