代码拉取完成,页面将自动刷新
// Time: O(n)
// Space: O(p), p is the number of paths
class Solution {
public:
int pathSum(vector<int>& nums) {
if (nums.empty()) {
return 0;
}
int result = 0;
queue<node> q;
node dummy(10);
auto parent_ptr = &dummy;
for (const auto& num : nums) {
node child(num);
while (!parent_ptr->isParent(child)) {
result += parent_ptr->leaf ? parent_ptr->val : 0;
parent_ptr = &q.front();
q.pop();
}
parent_ptr->leaf = false;
child.val += parent_ptr->val;
q.emplace(move(child));
}
while (!q.empty()) {
result += q.front().val;
q.pop();
}
return result;
}
private:
struct node {
int level, i, val;
bool leaf;
node(int n) : level(n / 100 - 1), i((n % 100) / 10 - 1), val(n % 10), leaf(true) {};
inline bool isParent(const node& other) {
return level == other.level - 1 && i == other.i / 2;
};
};
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。