Given a nested list of integers, returns the sum of all integers in the list weighted by their depth
For example, given the list {{1,1},2,{1,1}} the function should return 10 (four 1's at depth 2, one 2 at depth 1)
Given the list {1,{4,{6}}} the function should return 27 (one 1 at depth 1, one 4 at depth 2, one 6 at depth2)
/** * This is the interface that represents nested lists. * You should not implement it, or speculate about its implementation. */ public interface NestedInteger { //Returns true if this NestedInteger holds a single integer, rather than a nested list public boolean isInteger(); //Returns the single integer that the NestedInteger holds, if it holds a single integer //Returns null if this NestedInteger holds a nested list public Integer getInteger(); //Returns the nested list that this NestedInteger holds, if it holds a nested list //Returns null if this NestedInteger holds a single integer public List<NestedInteger> getList(); }
Solution:
public int sumOfNestedInteger(NestedInteger nest) { if(nest.isInteger()) { return nest.getInteger(); } return sumList(nest.getList(), 1); } private int sumList(List<NestedInteger> list, int depth) { int sum = 0; for(NestedInteger item: list) { if(item.isInteger()) { sum += item.getInteger()*depth; } else { sum += sumList(item.getList(), depth+1); } } return sum; }
Follow Up:
followup说改成return the sum of all integers in the list weighted by their “reversed depth”.
也就是说{{1,1},2,{1,1}}的结果是(1+1+1+1)*1+2*2=8
思路:
需要两个变量计数,sum 与 prev
例子 {{2,2,{3}},1,{2,2}}
一共三层
第一层 prev = 1 sum=1
第二层 prev =prev+2+2+2+2 最后prev =9, sum = 10
第三层 prev =prev +3 prev = 12 sum =22
理论结果 3+2*2*4+1*3 =22
public int sumOfReversedWeight(NestedInteger ni) { if(ni.isInteger()) { return ni.getInteger(); } int prev = 0, sum = 0; List<NestedInteger> cur = ni.getList(); List<NestedInteger> next = new ArrayList<>(); while(!cur.isEmpty()) { for(NestedInteger item:cur) { if(item.isInteger()) { prev += item.getInteger(); } else { next.addAll(item.getList()); } sum += prev; cur = next; next = new ArrayList<>(); } } return sum; }
Reference:
http://www.careercup.com/question?id=5139875124740096
http://www.mitbbs.com/article_t1/JobHunting/32850869_0_1.html
相关推荐
LinkedIn-Ads-Guide-2020.pdf
Flagship-linkedin-release-campaign2.apk
中国B2B品牌全球化营销白皮书-Linkedin-8-86页.pdf
前端开源库-passport-linkedin-oauth2Passport-LinkedIn-OAuth2,LinkedIn OAuth2 API v2的Passport
聪明的浏览器插件为LinkedIn简化了生成和业务开发。 关于Dux-Soup:https://www.dux-soup.com Dux-Soup使您可以轻松地在LinkedIn上找到,吸引和吸引潜在客户。 它会自动查看潜在客户简介,认可技能,跟踪活动并代表...
cd linkedin-profile-info-api npm install 跑步 npm start 终点 GET /info/:username从给定的LinkedIn用户名以JSON格式返回配置文件信息 配置 应用程序使用的环境变量 PORT -公开API服务器的端口 PROXY_URL出站请求...
领英python技能测试,搜集了30道原题,基本上15道里面能遇上一半原题。测试之前看一遍基本上稳过。奥利给!
类似于LinkedIn的软件的前端 具有基本功能的LinkedIn软件的克隆。 演示版 请在查看Netlify演示。 入门 这些说明将为您提供在本地计算机上运行并运行的项目的副本,以进行开发和... docker exec -it linkedin-react_l
向LinkedIn添加其他工具,包括连接消息预设和各个用户注释。 此扩展程序可帮助您通过便笺系统记住与个人有关的信息,该信息系统已同步到您的Google帐户,并且仅对您可见。 它还允许拥有易于访问的连接消息预设,并...
Linkedin测验答案,技能评估测试 该资料库适合那些寻求LinkedIn评估测验问题答案或愿意通过参与测试来帮助他人的人。 或者,您可能想创建您的第一个拉取请求并被添加为贡献者。 无论您的目标是什么-在这里始终欢迎您...
LinkedIn 自动连接工具 ... 它会安装两个二进制文件: linkedin-auto-bot和lac到您的系统路径。 用法 使用lac命令按如下方式使用它: $ lab -u enter_your_linkedin_email > -k Enter LinkedIn password: ***
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
linkedin-trelloLinkedIn到Trello连接器,以遵循您的招聘流程的状态Accross您的组织链接到... linkedin-trelloLinkedIn到Trello连接器,以遵循您的招聘流程的状态Accross您的组织链接到维基。 ...
LinkedIn Unfollower-取消关注不再感兴趣的人 您关注大量的人,似乎无法从您真正想要关注的人那里获得高质量的内容? 选项:-取消关注您正在关注的人员,公司,网络外连接。 -取消关注正在关注您的人。 -忽略收到的...
中国B2B品牌全球化营销白皮书-Linkedin-2019.8-86页.pdf
Linkedin-exchange-tokens 一个简单的函数,用于将 LinkedIn JSAPI 提供的短期 OAuth2 令牌交换为服务器可以用来进行 REST api 调用的长期 OAuth 令牌。 此机制在此该模块还有一种方法可以使 REST API 调用链接到,...
cd linkedin-export-tool npm install 配置 mkdir export cp config.json.dist config.json vim config.json 获取API密钥: 来自: 在哪里可以找到我的API密钥? 单击顶部导航菜单中的链接,以管理您的LinkedIn...