Careercup - Microsoft面试题 - 5799446021406720
2014-05-12 07:17
原题:
Given below is a tree/trie
A
B c D
e F
a<b<e<>>c<>d<f<>>>
above string represents the following trie/tree (visualize) and assume that there exisits a serialize method that performs above.
Now, write a deserialize method so that above string to an object model of the following
TreeNode
TreeNode[] children
题目:给定以上的字典树序列化方法,请写出相应的反序列化方法。
解法:观察序列化的字符串,可以发现序列化的规则是“字母<若干个子树>”。用一个栈就可以写出比较简洁的反序列化代码。
代码:
// http://www.careercup.com/question?id=5799446021406720
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std; struct TrieNode {
char ch;
vector<TrieNode *> child;
TrieNode(char _ch = ): ch(_ch), child(vector<TrieNode *>()) {};
}; TrieNode *deserializeFromString(const string &str)
{
TrieNode *root;
TrieNode *ptr;
stack<TrieNode *> st; int i, n; root = nullptr;
n = (int)str.length();
i = ;
while (i < n) {
if (str[i] == '>') {
st.pop();
++i;
} else {
ptr = new TrieNode(str[i]);
i += ;
if (st.empty()) {
root = ptr;
} else {
(st.top()->child).push_back(ptr);
}
st.push(ptr);
}
} return root;
} void preorderTraversal(TrieNode *root)
{
if (root == nullptr) {
return;
}
cout << root->ch << ' ';
for (int i = ; i < (int)root->child.size(); ++i) {
preorderTraversal(root->child[i]);
}
} int main()
{
TrieNode *root = nullptr;
string str; while (cin >> str) {
root = deserializeFromString(str);
preorderTraversal(root);
cout << endl;
} return ;
}
Careercup - Microsoft面试题 - 5799446021406720的更多相关文章
- Careercup - Microsoft面试题 - 6314866323226624
2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...
- Careercup - Microsoft面试题 - 6366101810184192
2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...
- Careercup - Microsoft面试题 - 24308662
2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...
- Careercup - Microsoft面试题 - 5700293077499904
2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...
- Careercup - Microsoft面试题 - 5204967652589568
2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...
- Careercup - Microsoft面试题 - 5175246478901248
2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...
- Careercup - Microsoft面试题 - 5718181884723200
2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...
- Careercup - Microsoft面试题 - 5173689888800768
2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...
- Careercup - Microsoft面试题 - 6282862240202752
2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...
随机推荐
- ORACLE 数据库的级联查询 一句sql搞定(部门多级)
在ORACLE 数据库中有一种方法可以实现级联查询 select * //要查询的字段 from table //具有子接点ID与父接点I ...
- vue-cli建立的项目如何在手机端运行以及如何用charles来抓包
刚开始自己在config文件夹下的index.js中的dev下的host写成的是localhost,但是发现自己不能在手机端访问,并且也不可以在charles进行抓包处理,后来把localhost改成 ...
- vs移动团队项目集合
vs移动团队项目集合: https://msdn.microsoft.com/zh-cn/library/vs/alm/dd936138(v=vs.120)/css
- jQuery-显示与隐藏不用判断的快捷方法
功能:显示被隐藏的元素,隐藏已显示的元素. 常规方法:(需要先判断元素状态) $("button").click(function(){ if ($(".content& ...
- C# asp.net mvc 注解验证
看代码,看注解,看懂了单词,没看懂意思. 今日只能专攻一下这项特性. 1.Remote 在看这个例子的时候 ,看了JsonResult 以及 JsonRequestBehavior.AllowGet解 ...
- C++ 关于运算符重载
转载来源:http://c.biancheng.net/cpp/biancheng/view/216.html 重载运算符的函数一般格式如下: 函数类型 operator 运算符名称 (形参表列 ...
- 【转载】2018 hosts 持续更新访问 gu歌【更新于:2018-05-03】
修改HOSTS实现免费,简单访问谷歌的目的 也是比较稳定的方法.修改hosts.修改hosts的方法,原理在于直接存储谷歌网站的IP地址.这样就不用DNS来解析网址了.也就是说,当我们输入谷歌 ...
- cuda流测试=basic_single_stream
cuda流测试 /* * Copyright 1993-2010 NVIDIA Corporation. All rights reserved. * * NVIDIA Corporation and ...
- Javascript的数据类型和转换
JavaScript 数据类型 在 JavaScript 中有 5 种不同的数据类型: string number boolean object function 3 种对象类型: Object Da ...
- Oracle 数字处理函数
数字处理函数 ① mod(number1,number2) 取余数的函数,比如mod(10,3) = 10/3 = 1. ② round(number,num_ditigs) .trunk(numbe ...