avatar
g面经来一个。# JobHunting - 待字闺中
t*r
1
Given a list which can can contain elements as well as lists, write an
iterator to flatten a nested list.
avatar
e*a
2
it's not difficult
avatar
m*3
3
用c++很难写啊,关键是c++怎么表示一个nested list,有什么好方法么?
avatar
b*w
4
type 'a elem = Single of 'a | Multi of ('a elem list);;
let rec flatten l =
match l with
| [] -> []
| hd :: tl -> match hd with
| Single x -> x :: flatten tl
| Multi x -> (flatten x) @ flatten tl;;
avatar
e*2
5
看起来像Ocaml,Haskel之类的。

【在 b*******w 的大作中提到】
: type 'a elem = Single of 'a | Multi of ('a elem list);;
: let rec flatten l =
: match l with
: | [] -> []
: | hd :: tl -> match hd with
: | Single x -> x :: flatten tl
: | Multi x -> (flatten x) @ flatten tl;;

avatar
s*x
6

C++ 怎么表示一个tree node? 用pointer 呗。
用 subclass, pointer to the base class 也可以。

【在 m******3 的大作中提到】
: 用c++很难写啊,关键是c++怎么表示一个nested list,有什么好方法么?
avatar
h*k
7
每个node是一个class object,class含有两个field,一个是list,一个是element
value

【在 m******3 的大作中提到】
: 用c++很难写啊,关键是c++怎么表示一个nested list,有什么好方法么?
avatar
e*y
8
格式都坏了,凑合看吧
这是airbnb的电面题
// [123,456,[788,799,833],[[]],10,[]]
// 123->456->788(list)->[](list)->10->(list)
// 799
// 833
class nestedList {
private:
bool isNumber;
list l;
int val;
public:
nestedList(string, int&);
void print();
};
nestedList::nestedList(string s, int& index) {
if(index==s.length()) return;
// object is a number
if(isdigit(s[index])) {
size_t sz;
val=stoi(s.substr(index), &sz);
// cout << "index==" << index << endl;
// cout << "value==" << val << endl;
isNumber=true;
index+=sz;
return;
}
// object is a list
isNumber=false;
// s[index]=='[', s[index+1]==']'
if(s[index+1]==']') { // list is empty
index+=2;
return;
}
// s[index]=='[', s[index+1]==number or '[';
do {
index++; // skip '[' or ','
nestedList object(s, index);
l.push_back(object);
} while(s[index]==',');
// s[index]==']'
index++; // skip ']'
return;
}
void nestedList::print() {
// print the number
if(isNumber) {
cout << val;
return;
}
// print the list
cout << '[';
for(auto it=l.begin(); it!=l.end(); it++) {
it->print();
if(l.end()!=next(it,1)) cout << ',';
}
cout << ']';
}

【在 t**r 的大作中提到】
: Given a list which can can contain elements as well as lists, write an
: iterator to flatten a nested list.

avatar
k*g
9
struct Node {
int value;
struct Node *next;
struct Node *nested;
};
Node *h = get_list();
if (h->nested) { // then there is a nested list behind this node
h = h->nested;
} else { // otherwise there is no nested list behind it
h = h->next;
}

【在 m******3 的大作中提到】
: 用c++很难写啊,关键是c++怎么表示一个nested list,有什么好方法么?
avatar
j*3
10
这个不能用java写吧?python的题吧?
java的list不是有type限定么?

【在 t**r 的大作中提到】
: Given a list which can can contain elements as well as lists, write an
: iterator to flatten a nested list.

avatar
V*B
11
用C++11?
还是该用variant?

【在 e**y 的大作中提到】
: 格式都坏了,凑合看吧
: 这是airbnb的电面题
: // [123,456,[788,799,833],[[]],10,[]]
: // 123->456->788(list)->[](list)->10->(list)
: // 799
: // 833
: class nestedList {
: private:
: bool isNumber;
: list l;

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。