Redian新闻
>
Linkedin这道题用非递归该怎么写啊?
avatar
Linkedin这道题用非递归该怎么写啊?# JobHunting - 待字闺中
z*b
1
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, and *one 6 at depth 3)
public int depthSum (List input)
{
//Implement this function
}
/**
* This is the interface that allows for creating 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 this 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 getList();
}
avatar
h*0
2
这种不就直接扫一遍收工?还能再简单点吗?

list
,
holds
a

【在 z***b 的大作中提到】
: 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, and *one 6 at depth 3)
: public int depthSum (List input)
: {
: //Implement this function
: }

avatar
I*s
3
用queue.
int sum = 0;
queue > q;
q.push(pair(input, 1));
while (! q.empty()) {
List L = q.front().first;
int depth = q.front().second;
q.pop();
foreach (NestedInteger n : L) {
if (n.isInteger()) sum += n.getInteger() * depth;
else q.push(pair(n.getList(), depth+1));
}
}
return sum;
avatar
z*b
4
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, and *one 6 at depth 3)
public int depthSum (List input)
{
//Implement this function
}
/**
* This is the interface that allows for creating 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 this 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 getList();
}
avatar
h*0
5
这种不就直接扫一遍收工?还能再简单点吗?

list
,
holds
a

【在 z***b 的大作中提到】
: 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, and *one 6 at depth 3)
: public int depthSum (List input)
: {
: //Implement this function
: }

avatar
I*s
6
用queue.
int sum = 0;
queue > q;
q.push(pair(input, 1));
while (! q.empty()) {
List L = q.front().first;
int depth = q.front().second;
q.pop();
foreach (NestedInteger n : L) {
if (n.isInteger()) sum += n.getInteger() * depth;
else q.push(pair(n.getList(), depth+1));
}
}
return sum;
avatar
a*3
7
BFS吧,LS正解
avatar
s*5
8
递归转iterative用stack
public int depthSum2 (List input){
Stack> nstack = new Stack<>();
Stack dstack = new Stack<>();
nstack.add(input);
dstack.add(1);
int total = 0;
while(nstack!=null){
int depth = dstack.pop();
for(NestedInteger i: nstack.pop()){
if(i.isInteger()){
total += i.getInteger()*depth;
} else{
dstack.push(depth+1);
nstack.push(i.getList());
}
}
}
return total;
}
avatar
s*5
9
3L BFS 也ok,我的是DFS 反正都是traverse 一遍。
avatar
j*g
10
为毛要用stack和queue, int base = 0; 遇到"{" base++; 遇到"}" base--; 遇到数
字就乘base累加。
哦 不是字符串啊
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。