日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode 364. 加权嵌套序列和 II(重复叠加)

發(fā)布時(shí)間:2024/7/5 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode 364. 加权嵌套序列和 II(重复叠加) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 1. 題目
    • 2. 解題

1. 題目

給一個(gè)嵌套整數(shù)序列,請(qǐng)你返回每個(gè)數(shù)字在序列中的加權(quán)和,它們的權(quán)重由它們的深度決定。

序列中的每一個(gè)元素要么是一個(gè)整數(shù),要么是一個(gè)序列(這個(gè)序列中的每個(gè)元素也同樣是整數(shù)或序列)。

與 前一個(gè)問(wèn)題 不同的是,前一題的權(quán)重按照從根到葉逐一增加,而本題的權(quán)重從葉到根逐一增加。

也就是說(shuō),在本題中,葉子的權(quán)重為1,而根擁有最大的權(quán)重。

示例 1: 輸入: [[1,1],2,[1,1]] 輸出: 8 解釋: 四個(gè) 1 在深度為 1 的位置, 一個(gè) 2 在深度為 2 的位置。示例 2: 輸入: [1,[4,[6]]] 輸出: 17 解釋: 一個(gè) 1 在深度為 3 的位置, 一個(gè) 4 在深度為 2 的位置, 一個(gè) 6 在深度為 1 的位置。 1*3 + 4*2 + 6*1 = 17。

來(lái)源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/nested-list-weight-sum-ii
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。

2. 解題

類似題目:
LeetCode 339. 嵌套列表權(quán)重和(DFS)
LeetCode 5363. 做菜順序 hard

/*** // This is the interface that allows for creating nested lists.* // You should not implement it, or speculate about its implementation* class NestedInteger {* public:* // Constructor initializes an empty nested list.* NestedInteger();** // Constructor initializes a single integer.* NestedInteger(int value);** // Return true if this NestedInteger holds a single integer, rather than a nested list.* bool isInteger() const;** // Return the single integer that this NestedInteger holds, if it holds a single integer* // The result is undefined if this NestedInteger holds a nested list* int getInteger() const;** // Set this NestedInteger to hold a single integer.* void setInteger(int value);** // Set this NestedInteger to hold a nested list and adds a nested integer to it.* void add(const NestedInteger &ni);** // Return the nested list that this NestedInteger holds, if it holds a nested list* // The result is undefined if this NestedInteger holds a single integer* const vector<NestedInteger> &getList() const;* };*/ class Solution { public:int depthSumInverse(vector<NestedInteger>& nestedList) {int presum = 0, ans = 0, i;vector<NestedInteger> nextLevel;for(i = 0; i < nestedList.size(); ++i){if(nestedList[i].isInteger())presum += nestedList[i].getInteger();else{auto temp = nestedList[i].getList();for(auto& t : temp)nextLevel.push_back(t);}if(i == nestedList.size()-1){ans += presum;swap(nestedList,nextLevel);nextLevel.clear();i = -1;}}return ans;} };

4 ms 8.9 MB


我的CSDN博客地址 https://michael.blog.csdn.net/

長(zhǎng)按或掃碼關(guān)注我的公眾號(hào)(Michael阿明),一起加油、一起學(xué)習(xí)進(jìn)步!

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的LeetCode 364. 加权嵌套序列和 II(重复叠加)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。