z***b 发帖数: 127 | 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();
} | h*****0 发帖数: 4889 | 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 : }
| I**********s 发帖数: 441 | 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; | z***b 发帖数: 127 | 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();
} | h*****0 发帖数: 4889 | 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 : }
| I**********s 发帖数: 441 | 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; | a*******3 发帖数: 13 | | s**********5 发帖数: 19 | 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;
} | s**********5 发帖数: 19 | 9 3L BFS 也ok,我的是DFS 反正都是traverse 一遍。 | j********g 发帖数: 80 | 10 为毛要用stack和queue, int base = 0; 遇到"{" base++; 遇到"}" base--; 遇到数
字就乘base累加。
哦 不是字符串啊 |
|