• leetcode 666. Path Sum IV


    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digits integers.

    For each integer in this list:

    1. The hundreds digit represents the depth D of this node, 1 <= D <= 4.
    2. The tens digit represents the position P of this node in the level it belongs to, 1 <= P <= 8. The position is the same as that in a full binary tree.
    3. The units digit represents the value V of this node, 0 <= V <= 9.

    Given a list of ascending three-digits integers representing a binary with the depth smaller than 5. You need to return the sum of all paths from the root towards the leaves.

    Example 1:

    Input: [113, 215, 221]
    Output: 12
    Explanation: 
    The tree that the list represents is:
        3
       / 
      5   1
    
    The path sum is (3 + 5) + (3 + 1) = 12.
    

    Example 2:

    Input: [113, 221]
    Output: 4
    Explanation: 
    The tree that the list represents is: 
        3
         
          1
    
    The path sum is (3 + 1) = 4.

    这到题目要求每个叶子结点到根结点的路径上的权值和。我有几个想法,今天写了第一种想法的代码:

    第一种写法,每层用map记录节点在当前层位置和权值信息,然后根据孩子结点 = (父亲结点+1)/2来进行位置索引不断向下层进行权值相加,直到叶子结点。具体看代码

    class Solution {
    public:
        int pathSum(vector<int>& nums) {
            vector<map<int,int> > v;
            int cnt = 1;
            map<int, int> mp;
            for (int i = 0; i < nums.size(); ++i) {
                int x = nums[i]/100;
                if (x == cnt) {
                    mp[(nums[i]/10)%10] = nums[i]%10;
                } else {
                    v.emplace_back(mp);
                    cnt ++;
                    mp.clear();
                    mp[(nums[i]/10)%10] = nums[i]%10;
                }
            }
            v.emplace_back(mp);
            int sum = 0;
            for (int i = 1; i < v.size(); ++i) {
                map<int, int> tmp = v[i-1];
                for (auto &x :v[i]) {
                    x.second += tmp[(x.first+1)/2];
                }
            }
            for (int i = 0; i < v.size(); ++i) {
                if (i == v.size() - 1) {
                    for (auto x: v[i]) {
                        sum += x.second;
                    }
                } else {
                    map<int,int> tmp = v[i+1];
                    for (auto x: v[i]) {
                        if (!tmp[x.first*2] && !tmp[x.first*2-1])
                            sum += x.second;
                    }
                }
            }
            return sum;
        }
    };

    第二种方法:

    ....就是简化上面的代码,用一个数组代替map  同时不用把全部的结点信息存储,用两个数组滚动

    这是其他人的代码

    class Solution {
    public:
        int pathSum(vector<int>& nums) {
            if (nums.empty()) return 0;
            vector<int> tree(8, 0);
            int dep = nums.back()/100, n = nums.size(), ans = 0;
            for (int i = 1, j = 0; i <= dep; i++) {
                vector<int> tmp(8, 0);
                // fill path sum in nodes of each level
                while (j < n && nums[j]/100 == i) {
                    int pos = nums[j]%100/10-1;
                    tmp[pos] = nums[j++]%10 + tree[pos/2];
                }
                // if both children are null, add the parent leaf node
                // also work for node value of 0, such as 210
                for (int k = 0; k < 4; k++) {
                    if (tmp[k*2] == 0 && tmp[k*2+1] == 0)
                        ans += tree[k];
                }
                swap(tree, tmp);
            }
            return accumulate(tree.begin(), tree.end(), ans);
        }
    };
  • 相关阅读:
    数组顺序表
    指针、数组、结构体
    急救模式下安装rpm包
    如何杀死远程服务器到本机的tcp连接
    centos升级内核之后修改内核启动顺序
    rpm yum 等命令无响应的解决方法
    关于ssh 设置的相关总结(ssh最大连接数、ssh连接时长、安全性配置等)
    详解Linux中的日志及用日志来排查错误的方法
    linux 普通用户登陆系统su
    如何更新/升级Red Hat Enterprise Linux内核?
  • 原文地址:https://www.cnblogs.com/pk28/p/7445845.html
Copyright © 2020-2023  润新知