由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Linkedin这道题用非递归该怎么写啊?
相关主题
sum nested list 我连题目都没看懂T_T 求解答讨论一道L的validate binary tree和求深度的问题
问linkedin家一道题的followup请教两个算法题
求指点一道G家Iterator的题目Leetcode 的 Surrounded Regions 好难过大OJ (in JAVA)
问道面试题热腾腾g电面 已挂
发一下最近的几个面试面经,为接下来的onsite攒RPTree的traversal也分BFS和DFS?
问一道lyft design题,求大神!G家面经(已被HC挂,求分析)
请教一个binary tree问题leetcode做伤心了
有人面过linkedin,比google amazon 题目怎么样?讨论一个g题
相关话题的讨论汇总
话题: list话题: depth话题: returns话题: holds
进入JobHunting版参与讨论
1 (共1页)
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
7
BFS吧,LS正解
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累加。
哦 不是字符串啊
1 (共1页)
进入JobHunting版参与讨论
相关主题
讨论一个g题发一下最近的几个面试面经,为接下来的onsite攒RP
请问这道题如何做?Zero-one multiple问一道lyft design题,求大神!
leetcode number of islands为什么不能用BFS?请教一个binary tree问题
leetcode 新题 Course Schedule用BFS怎么做?有人面过linkedin,比google amazon 题目怎么样?
sum nested list 我连题目都没看懂T_T 求解答讨论一道L的validate binary tree和求深度的问题
问linkedin家一道题的followup请教两个算法题
求指点一道G家Iterator的题目Leetcode 的 Surrounded Regions 好难过大OJ (in JAVA)
问道面试题热腾腾g电面 已挂
相关话题的讨论汇总
话题: list话题: depth话题: returns话题: holds