a***e 发帖数: 413 | 1 写了很久都过不了,15分钟哪里能写完啊?能把思路说清楚就不错了。不知道什么样的
公司和面试官会出这种题。
https://oj.leetcode.com/problems/word-ladder-ii/
vector> findLadders(string start, string end, unordered_set<
string> &dict) {
vector> res;
vector> empty;
queue curLevel;
queue nextLevel;
vector path;
path.push_back(start);
curLevel.push(start);
res.push_back(path);
string word = start;
bool find = false;
int n = word.size();
bo... 阅读全帖 |
|
a***e 发帖数: 413 | 2 写了很久都过不了,15分钟哪里能写完啊?能把思路说清楚就不错了。不知道什么样的
公司和面试官会出这种题。
https://oj.leetcode.com/problems/word-ladder-ii/
vector> findLadders(string start, string end, unordered_set<
string> &dict) {
vector> res;
vector> empty;
queue curLevel;
queue nextLevel;
vector path;
path.push_back(start);
curLevel.push(start);
res.push_back(path);
string word = start;
bool find = false;
int n = word.size();
bo... 阅读全帖 |
|
a***e 发帖数: 413 | 3 还是开始没想清楚,发现用2个stack要清楚很多,2个vector就搞晕了。
Sigh,贴一个终于通过的明天再看再简化。每次碰到这种开始以为简单又老是写不对的
题就很有挫折感!
vector > zigzagLevelOrder(TreeNode *root) {
vector> ret;
vector levelVals;
stack curLevel;
stack nextLevel;
if (root==NULL)
return ret;
TreeNode *r = root;
curLevel.push(r);
bool l2r = true;
while(!curLevel.empty())
{
if (!l2r)
... 阅读全帖 |
|
h**********d 发帖数: 4313 | 4 1.如果是full tree,可以用递归
2.如果不是full tree,笨办法用level-order traversal遍历,同时把当前节点next指
向queue.peek(),复杂度O(N)
写个java版本
void populate(TreeNode root){
if(root == null)return;
Queue queue = new Queue();
queue.put(root);
Node node;
int thisLevel = 1; int nextLevel = 0;
while(!queue.isEmpty()){
node = queue.pop();
thisLevel--;
if(thisLevel != 0){
node.setNextRight(queue.peek());
}
if(node.getLeft() != null){
queue.put(node.getLeft());
nextLe... 阅读全帖 |
|
A*****o 发帖数: 284 | 5 贴个递归的完整代码,抛砖引玉了
#include
#include
#include
#include
using namespace std;
// Interview question: Rebuild a tree with DFS output with level
// A, 0, B, 1, C, 2, D, 2
struct TreeNode {
string id;
TreeNode(string a) {
id = a;
}
vector children;
};
void rebuildImpl(istringstream & iss, TreeNode *&father, string id, int
level) {
TreeNode *p = new TreeNode(id);
if (!father) {
father = p;
}
else {
... 阅读全帖 |
|
a***e 发帖数: 413 | 6 我照你说的写了一个,还是比较长,但的确要清楚了很多。请问你能share一个你的么
?多谢!
我感觉如果不是特别熟,再加上现场一紧张或者发晕,很难把这么多行程序在15分钟内
bugfree的写完,汗。
vector > zigzagLevelOrder(TreeNode *root) {
queue nextLevel;
queue curLevel;
TreeNode *r = root;
vector > ret;
if (r==NULL)
return ret;
curLevel.push(r);
bool l2r=true;
while(1)
{
vector tmpV;
wh... 阅读全帖 |
|
z*********e 发帖数: 10149 | 7 现在会超时,哪个地方应该优化一下?用的BFS
本机上一秒之内就出结果了,应该没有严重的算法问题吧
public List> findLadders(String start, String end, Set
dict) {
List> lastPaths = new ArrayList<>();
if(start == null || end == null) return lastPaths;
dict.add(end);
List top = new ArrayList<>();
top.add(start);
lastPaths.add(top);
if(start.equals(end)) return lastPaths;
int wordLen = start.length();
while(!dict.isEmpty()){ // same as ... 阅读全帖 |
|
y*********e 发帖数: 518 | 8 分层遍历,也可以用 vector 。
void levelTraverse(Tree root) {
Vector thisLevel = new Vector();
Vector nextLevel = new Vector();
thisLevel.add(root);
while (!thisLevel.isEmpty()) {
foreach (Tree tree in thisLevel) {
print(tree);
nextLevel.addAll(tree.children());
}
thisLevel.clear();
thisLevel.addAll(nextLevel);
nextLevel.clear();
println();
}
} |
|
l*****a 发帖数: 14598 | 9 刚才面了个contractor
面了道简单的,print binary tree level by level
为节省时间,直接让他去collabedit,我干我的工作
class node{
private int data;
private Node left;
private Node right;
private node parent;
private int level;
public LinkedList listofNode;
public int getData()
{
return this.data;
}
public node getLeftNode()
{
return this.left;
}
public node getRightNode()
{
return this.right;
}
public printTree(Node node )
{
... 阅读全帖 |
|
f********c 发帖数: 147 | 10 这题试了好几个办法总是通不过,按说不难的,求帮忙看看,谢谢!
public class Solution {
public List> levelOrder(TreeNode root) {
List> result = new ArrayList>();
if(root == null) return result;
Queue current = new LinkedList(); //store
current level TreeNode
current.add(root);
List levelValue = new ArrayList(); //store
elements in current level
while(!current.isEmpty()) {
Queue阅读全帖 |
|
i**********e 发帖数: 1145 | 11 For question no. 6 (Binary tree serialization),
Writing the tree to a file is called 'serialization' and reading back from
the file to reconstruct the exact same binary tree is 'deserialization'.
You can use pre-order to traverse the tree one time to do the serialization.
The other method would be using a level-order traversal.
http://www.ihas1337code.com/2010/09/serializationdeserialization-of-binary.html
Question no. 12 you can either use 2 queues, currentLevel and nextLevel. Pop
nodes off cur... 阅读全帖 |
|
j********r 发帖数: 25 | 12 没有时间看你的是什么问题, 你参考一下如下程序吧:
int ladderLength(string start, string end, unordered_set &dict) {
int remaining = 1;
queue qou;
qou.push(start);
dict.erase(start);
int depth = 0;
while(qou.size() > 0) {
depth++;
int nextLevel = 0;
while(remaining >0) {
string s = qou.front();
qou.pop();
if (s == end) { return depth;}
... 阅读全帖 |
|
c********l 发帖数: 8138 | 13 //MITBBS' article system will mess up the source code
// You may probably need to re-arrange the line breaks
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Solution {
public static void main(String[] args) {
Solution sol = new Solution();
String[] dictStrArr = new String[] {
"dose","ends","dine","jars","prow","soap","guns","hops","
cray","hove","ella","hour","lens","j... 阅读全帖 |
|
c********l 发帖数: 8138 | 14 简洁不是最终极的目标,有时候需要reduce runtime complexity
见我之前写的
http://www.mitbbs.com/article/JobHunting/32658475_3.html
//MITBBS' article system will mess up the source code
// You may probably need to re-arrange the line breaks
// by coupondeal@mitbbs
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Solution {
public static void main(String[] args) {
Solution sol = new Solution();
String[] dictStrA... 阅读全帖 |
|
O*******d 发帖数: 20343 | 15 我用的就是在现在的C++里用recursion. 极度简化后就是下边的思路
template
class Fool
{
public:
Fool() { data = N; }
unsigned int data;
Fool nextLevel; // recursively create next level.
void printData() { cout << data; nextLevel.printData(); }
};
// Specialize to terminate recursion
template<>
class Fool<0U>
{
public:
Fool() { data = 0U; }
unsigned int data;
void printData() { cout << data; }
};
Fool<9U> fool;
fool.printData(); // print 987654321... 阅读全帖 |
|
l*******g 发帖数: 8 | 16 一个queue应该就可以了,我把code贴出来了。如果有错误的话,请指正。
printByLevel(Node *root)
{
Queue queue;
int currLevel = 1; /* total number of nodes on the current level */
int currPos = 0; /* current position on the current level */
int nextLevel = 0; /* total number of nodes on the next level */
queue.enqueue(root);
while(currLevel>0)
{
Node *curr = queue.dequeue();
printNode(curr);
if(curr->left){
queue.enqueue(curr->left);
nextL |
|
q****m 发帖数: 177 | 17 没有考虑 nextLevel < level的情况 |
|
c********l 发帖数: 8138 | 18 回过头看了一下,
思路没问题
把currLevel和nextLevel的类型从ArrayList改成HashSet就可以了
那个5字母的input case ,没改之前是公版的3倍,
改了HashSet 就比公版快2 ms
WTF |
|
O**e 发帖数: 569 | 19
Customer Review
130 of 134 people found the following review helpful
2.0 out of 5 stars Ho Hum, April 26, 2015
By Nextlevel
This review is from: Dyson V6 Absolute Cordless Vacuum (Misc.)
We purchased our V6 at Costco. It was exactly what we were looking for. A
light weight easy to vacuum. Cordless with great attachments. We have a
dyson canister vac that we really like. It's just a little big to pull out
everyday and get up a little dog hair and dust. The vacuum performed
wonderfully.
The batte... 阅读全帖 |
|