由买买提看人间百态

topics

全部话题 - 话题: voided
首页 上页 1 2 3 4 5 6 7 8 9 10 (共10页)
r******n
发帖数: 170
1
来自主题: JobHunting版 - 请教一道面试题,关于tree的
贴个tree的版本,不过空string对应空tree的时候,似乎空指针处理的有些ugly,求更
简洁版本。
struct BNode
{
char c;
BNode* left;
BNode* right;
BNode():c(0){}; //avoid uninitialized value for c
BNode(char _c):c(_c),left(NULL),right(NULL){};
};
void pre2bst(string pre, size_t& start, size_t len, BNode* &p)
{
if(start {
p=new BNode(pre[start]);
size_t curr=start;
start++;
if(pre[curr]=='+'||pre[curr]=='-'||pre[curr]=='*'||pre[curr]=='/')
{
pre2bst(pre,s... 阅读全帖
A**u
发帖数: 2458
2
来自主题: JobHunting版 - 算法题求教
这个题目我刚好练过
这里有介绍
http://www.geeksforgeeks.org/archives/14469
用backtracking.
关键是,这个树的有些node, 可以不用访问
比如 你要找9.
假设从1开始那么,到达(1,2,4)node.
(1,2,4)的和 加上 {5,7,8}的最小值 > 9.
所以不用考虑 包含1,2,4的node了,这样可以可以排序不少点
这是我的算法
#include
#include
#include
using namespace std;
void re(int depth, int sum, vector selected,int w[], int N, int target){
if ( depth == N )
{
if ( sum == target )
{
for(vector::iterator it = selected.begin(); it != selecte... 阅读全帖
a******u
发帖数: 239
3
来自主题: JobHunting版 - c interview question
I think my answers to Question 7 and 8 are correct, but they said they are
not correct, why?
I already tested the code by Visual C++ (c compiler).
Thank you very much.
Line in file Contents
30 int * someIDs, theFirst, *r;
110 someIDs =GetSomeIDs(); /* defined below */
111 theFirst = someIDs [0];
112 r= ReorderIDs(someIDs);
113-150 /* we want to use ‘theFirst’ and ‘r’ here*/

499 /*-------- GetSomeIDs-----*/
500 int * GetSomeIDs()
501 {
502 int ids[8];
503-5... 阅读全帖
f*******t
发帖数: 7549
4
来自主题: JobHunting版 - 不用暴力,这道题有没有优化解
写了很长的代码,感觉不太对,如果一个数组里面有重复的数,应该会输出重复的组合
,但程序实际运行结果貌似是没有,不知道为什么。
#include
#include
#include
#include
#include
#define N 5
using namespace std;
struct combination {
int indices[N];
int sum;
combination();
bool operator<(const combination& c) const;
void operator=(const combination& c);
bool operator() (const combination& c) const;
bool operator== (const combination& c) const;
};
combination::combination()
{
sum = 0;
f... 阅读全帖
p*****2
发帖数: 21240
5
void combine( string str ){
int length = str.Length;
char[] instr = str.ToCharArray();
StringBuilder outstr = new StringBuilder();
doCombine( instr, outstr, length, 0, 0 );
}
void doCombine( char[] instr, StringBuilder outstr, int length,
int level, int start ){
for( int i = start; i < length; i++ ){
outstr.Append( instr[i] );
Console.WriteLine( outstr );
if( i < length - 1 ){
doCombine( instr, outstr, lengt... 阅读全帖
d****o
发帖数: 1055
6
来自主题: JobHunting版 - 一道OO设计题
一个人的interface,另外几个类继承
class PersonInterface{
private:
Eye eye;
Ear ear;
Mouth mouth;
Leg leg;
public:
void useEye(Light light);
void useEar(Sound sound);
Sound useMouth()=0;
void disableEye(){
eye.setFunction(false);
}
void ableEye(){
eye.setFunction(true);
}
//其余两个ear和mouth一样的
}
class ordinaryPerson: public PersonInterface{
}
class blindPerson: public PersonInterface{
public:
blindPerson(){
eye.disableEye();
}
... 阅读全帖
c*****e
发帖数: 737
7
来自主题: JobHunting版 - 最新微软SDE II面试题
1, class A {virtual void f();}
class B:public A {};
class C:public B {};
class D:public A, B {virtual void f();}
让你说明D的virutal table
2, class A
{public:
A(int v) {var = v;}
void f(){cout << var << endl;}
int var;
}
class B:public A
{
public:
public B(int v1, v2){var = v1; var2 = v2;}
void f()
{cout << var << "," << var2 << endl;}
int var, var2;
}
main()
{
B* pb = static_cast(new A(1);
pb... 阅读全帖
c*****e
发帖数: 737
8
来自主题: JobHunting版 - 微软C++面试题
1 #include
2
3 class A
4 {
5 public:
6 A(int v_) : v(v_) {};
7 virtual void foo() {std::cout << "A:" << v << "\n";}
8 int v;
9 };
10
11 class B : public A
12 {
13 public:
14 B(int w_, int z_):w(w_), A(z_){};
15 virtual void foo() {std::cout << "B:" << w << "\n";}
16 int w;
17 };
18
19 class C : public A
20 {
21 public:
22 C(int k): A(k){};
23 v... 阅读全帖
f*******t
发帖数: 7549
9
来自主题: JobHunting版 - amazon面试题目讨论贴2
#include
#include
#include
#define ENTRYNOTFOUND 0
#define ENTRYFOUND 1
#define NOTCOMPLETE 2
struct Trie;
void FindWords(Trie *root);
struct Trie
{
Trie();
~Trie();
Trie *children[26];
bool isEnd;
};
Trie::Trie()
{
for(int i = 0; i < 26; i++)
children[i] = NULL;
isEnd = false;
}
Trie::~Trie()
{
for(int i = 0; i < 26; i++) {
if(children[i]) {
delete children[i];
children[i] = NULL;
}
}
}
... 阅读全帖
l*********8
发帖数: 4642
10
来自主题: JobHunting版 - Text Justification
这个题目很容易出错啊。
我写在纸上的程序好几个bugs。
加上在电脑的调试修改的时间,我总共花了两个小时才让程序基本正确(还不敢保证百分百正确,可能需要更多的测试案例)。这么慢怎么面试啊?
下面是程序(测试程序就不贴了):
void JustifyOneLine(const vector & words, int L, int lineStart, int
& lineEnd, vector & blankNum)
{
blankNum.clear();
int lengthSum = words[lineStart].size();
for (lineEnd = lineStart+1; lineEnd < words.size() && lengthSum + 1 +
words[lineEnd].size() <= L; lineEnd++) {
lengthSum += 1 + words[lineEnd].size();
blankNum.push_back(1);
}
int ... 阅读全帖
l*********8
发帖数: 4642
11
来自主题: JobHunting版 - Text Justification
这个题目很容易出错啊。
我写在纸上的程序好几个bugs。
加上在电脑的调试修改的时间,我总共花了两个小时才让程序基本正确(还不敢保证百分百正确,可能需要更多的测试案例)。这么慢怎么面试啊?
下面是程序(测试程序就不贴了):
void JustifyOneLine(const vector & words, int L, int lineStart, int
& lineEnd, vector & blankNum)
{
blankNum.clear();
int lengthSum = words[lineStart].size();
for (lineEnd = lineStart+1; lineEnd < words.size() && lengthSum + 1 +
words[lineEnd].size() <= L; lineEnd++) {
lengthSum += 1 + words[lineEnd].size();
blankNum.push_back(1);
}
int ... 阅读全帖
a******g
发帖数: 13519
12
Please read and follow all instructions before beginning. For questions 1-4,
answer any two questions in any of the following programming languages (C /
C++ / Java / C# / VB / VB .NET). Please read questions carefully and answer
them to the BEST of your availability.
1. Write a function that takes as arguments two singly linked lists(unsorted
) and returns a singly linked list with nodes that exist in only one of the
input linked lists.
2. Write a function that returns the greatest product of fi... 阅读全帖
w********s
发帖数: 1570
13
来自主题: JobHunting版 - select2perform上面C++测试挺头疼的
很多stl的问题,主要是std::remove_if之类的
还有语法问题主要是考exception, 继承
举个例子,
class A
{
virtual void f() throw (int, double, long) = 0;
}
class B
{
int f();//1
int f() throw(int);//2
void f() throw(double, long, int);//3
void f();//4
void f() throw(long);//5
}
哪些是对的
b******g
发帖数: 77
14
struct Node
{
Value value;
Node * left;
Node * right;
}
void printBT(Node * root);
void printBT(vector & v1);
void printBT(Node * root)
{
vector v1;
v1.push_back(root);
printBT(v1);
}
// recursively printBT Date structure: using two vectors -- v1 holds nodes
of current level, v2 hold nodes of next level;
void printBT(vector & v1)
{
if ( v1.empty() )
return;
vector v2;
Node * node;
for (int i = 0; i < v1.size(); i++... 阅读全帖
w****x
发帖数: 2483
15
来自主题: JobHunting版 - 几道F家面试题
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE(int x) : nVal(x), pLft(NULL), pRgt(NULL) {}
};
struct TreeIter
{
stack m_stk;
void movNext()
{
if (m_stk.empty())
return;
NODE* pCur = m_stk.top();
if (pCur->pRgt != NULL)
{
pushLft(pCur->pRgt);
return;
}

while (!m_stk.empty() && pCur != m_stk.top()->pLft)
{
pCur = m_stk.top();
m_stk.pop... 阅读全帖
s********k
发帖数: 6180
16
来自主题: JobHunting版 - google和twitter的onsite面经
读写锁要求是什么?是不是很多读比较少写,read可以同时进行?但是写的时候所有读
都被block?
按照qt的reference,写了一个
class ReadWriteMutex
{
public:
ReadWriteMutex(int maxReaders = 32)
: semaphore(maxReaders)
{
}

void lockRead() { semaphore++; }
void unlockRead() { semaphore--; }
void lockWrite() {
QMutexLocker locker(&mutex);
for (int i = 0; i < maxReaders(); ++i)
semaphore++;
}
void unlockWrite() { semaphore -= sem... 阅读全帖
s********k
发帖数: 6180
17
来自主题: JobHunting版 - google和twitter的onsite面经
读写锁要求是什么?是不是很多读比较少写,read可以同时进行?但是写的时候所有读
都被block?
按照qt的reference,写了一个
class ReadWriteMutex
{
public:
ReadWriteMutex(int maxReaders = 32)
: semaphore(maxReaders)
{
}

void lockRead() { semaphore++; }
void unlockRead() { semaphore--; }
void lockWrite() {
QMutexLocker locker(&mutex);
for (int i = 0; i < maxReaders(); ++i)
semaphore++;
}
void unlockWrite() { semaphore -= sem... 阅读全帖
S**I
发帖数: 15689
18
☆─────────────────────────────────────☆
alexsung (alexsung) 于 (Wed Apr 11 13:52:02 2012, 美东) 提到:
Please read and follow all instructions before beginning. For questions 1-4,
answer any two questions in any of the following programming languages (C /
C++ / Java / C# / VB / VB .NET). Please read questions carefully and answer
them to the BEST of your availability.
1. Write a function that takes as arguments two singly linked lists(unsorted
) and returns a singly linked list with nodes that ex... 阅读全帖
r*c
发帖数: 167
19
来自主题: JobHunting版 - L家电面
二爷,坑爹算法来也。用状态机搞砸一切面面!哈哈
#include
#include
#include
using namespace std;
class Numericality;
void TestNumericality();
void TestNumericalityHelper(const string& str, const bool expected,
Numericality& nc);
class Numericality
{
public:
struct State
{
const string _str;
int _index;
State() {}
State(string s, int i) : _str(s), _index(i){}
virtual State* MoveNext(){
if(_str.empty() || _index < 0 || _index >= _str... 阅读全帖
w****x
发帖数: 2483
20
来自主题: JobHunting版 - 哪里有mutex和semaphore例子?
mutex和semaphore实现读写锁
class ReadWriteMutex
{
public:
ReadWriteMutex(int maxReaders = 32)
: semaphore(maxReaders)
{
}

void lockRead() { semaphore++; }
void unlockRead() { semaphore--; }
void lockWrite() {
QMutexLocker locker(&mutex);
for (int i = 0; i < maxReaders(); ++i)
semaphore++;
}
void unlockWrite() { semaphore -= semaphore.total(); }
int maxReaders() const ... 阅读全帖
I**********s
发帖数: 441
21
最喜欢 wwwyhx的解法: recursive descent.
我也写了个, 用的是建造AST, 再evaluate AST. 应该相当完整了: 数字之前可以有+-
号, 中间可以有一个小数点. 数字和运算符号之间可以有空格. 可以使用+,-,*,/,^,以
及括号. 可以检测错误输入并报出错误位置. 就是比较长, 不适合面试用. 供大家参考.
#include
#include // for pow()
using namespace std;
struct Node {
double val;
char op;
int op_prec; // precedence of operator
int type; // 0 - operand, 1 - operator
Node * left;
Node * right;
Node(double _val, char _op, int _type) : val(_val), op(_op),
type(_type), lef... 阅读全帖
I**********s
发帖数: 441
22
最喜欢 wwwyhx的解法: recursive descent.
我也写了个, 用的是建造AST, 再evaluate AST. 应该相当完整了: 数字之前可以有+-
号, 中间可以有一个小数点. 数字和运算符号之间可以有空格. 可以使用+,-,*,/,^,以
及括号. 可以检测错误输入并报出错误位置. 就是比较长, 不适合面试用. 供大家参考.
#include
#include // for pow()
using namespace std;
struct Node {
double val;
char op;
int op_prec; // precedence of operator
int type; // 0 - operand, 1 - operator
Node * left;
Node * right;
Node(double _val, char _op, int _type) : val(_val), op(_op),
type(_type), lef... 阅读全帖
w****x
发帖数: 2483
23
来自主题: JobHunting版 - read-write locker的实现
class ReadWriteMutex
{
public:
ReadWriteMutex(int maxReaders = 32)
: semaphore(maxReaders)
{
}

void lockRead() { semaphore++; }
void unlockRead() { semaphore--; }
void lockWrite() {
QMutexLocker locker(&mutex);
for (int i = 0; i < maxReaders(); ++i)
semaphore++;
}
void unlockWrite() { semaphore -= semaphore.total(); }
int maxReaders() const { return semaphore.to... 阅读全帖
j*****y
发帖数: 1071
24
来自主题: JobHunting版 - read-write locker的实现
有 write的时候 要block read, 这个清楚
有 read的时候,如果要 write, 需要block正在read的线程吗?还是等这些
正在 read的线程 释放 read-write lock后再 write ?

class ReadWriteMutex
{
public:
ReadWriteMutex(int maxReaders = 32)
: semaphore(maxReaders)
{
}

void lockRead() { semaphore++; }
void unlockRead() { semaphore--; }
void lockWrite() {
QMutexLocker locker(&mutex);
for (int i = 0; i < maxReaders(); ++i)
semaphore++;
}
... 阅读全帖
x******a
发帖数: 6336
25
Thanks a lot! it worked.
再请教一个问题:我想历遍一个通过frontinsert生成的linkedlist,为什么看不到第
一个insert的元素?谢谢
#include
#include "singlylinkedlist.h"
int main(int argc, const char * argv[])
{
SingleLinkedList aList;
int i=0;
while(i<10){
aList.firstInsert(i);
++i;
}
aList.traverse();
}
输出是
9 8 7 6 5 4 3 2 1
没有0
template< typename T> class ListElement;
template< typename T> class SingleLinkedList;
template
class ListElement{
public:
ListElement(cons... 阅读全帖
w*r
发帖数: 72
26
来自主题: JobHunting版 - LinkedIn 面经
two semaphores 就够了吧
#include
#include
#include
class sem {
boost::mutex guard;
int c ;
public:
sem() : c(0) {}
void V ( int n = 1 ) {
boost::mutex::scoped_lock lock(guard);
c = c + n;
}
void P ( int n = 1) {
while ( c < n ) ;
boost::mutex::scoped_lock lock(guard);
c = c - n;
}

};
sem semh;
sem semo;
class H{
public:
void operator()() {
std::cout << "create H thread ... 阅读全帖
w*r
发帖数: 72
27
来自主题: JobHunting版 - LinkedIn 面经
two semaphores 就够了吧
#include
#include
#include
class sem {
boost::mutex guard;
int c ;
public:
sem() : c(0) {}
void V ( int n = 1 ) {
boost::mutex::scoped_lock lock(guard);
c = c + n;
}
void P ( int n = 1) {
while ( c < n ) ;
boost::mutex::scoped_lock lock(guard);
c = c - n;
}

};
sem semh;
sem semo;
class H{
public:
void operator()() {
std::cout << "create H thread ... 阅读全帖
i*******6
发帖数: 107
28
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
My 5 cents:
import java.util.concurrent.*;
class MyO implements Runnable{
int index;
CountDownLatch lock;
public MyO (int index, CountDownLatch lock) {
this.index = index;
this.lock = lock;
}
public void run() {
try {
Thread.sleep((long) (Math.random() * 1000));
lock.await();
System.out.println("Take O:" + index + " to form an H2O!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
... 阅读全帖
i*******6
发帖数: 107
29
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O
My 5 cents:
import java.util.concurrent.*;
class MyO implements Runnable{
int index;
CountDownLatch lock;
public MyO (int index, CountDownLatch lock) {
this.index = index;
this.lock = lock;
}
public void run() {
try {
Thread.sleep((long) (Math.random() * 1000));
lock.await();
System.out.println("Take O:" + index + " to form an H2O!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
... 阅读全帖
s********x
发帖数: 914
30
来自主题: JobHunting版 - 狗狗家onsite面经
2吧
class CharNode {
private char value;
private CharNode next;
private CharNode previous;
public CharNode getPrevious() {
return previous;
}
public void setPrevious(CharNode previous) {
this.previous = previous;
}
public char getValue() {
return value;
}
public void setValue(char value) {
this.value = value;
}
public CharNode getNext() {
return next;
}
public void setNext(CharNode next) {
this... 阅读全帖
c********o
发帖数: 30
31
来自主题: JobHunting版 - 问一个java的基本问题
Final actually means that the reference can not change, not the content.
If you define
static final int[] a = [1, 2]; // let's say here a is pointing at address
0xffff,
int[] b ={5, 6} // here b is pointing at 0xCCCC
a = b; // this will give you error directly, as a cannot point to any other
address.
but a = {5, 6} is fine, as you can modify the content.
I just do some modification of your example, you use methods which contain
the int[] params:
import java.util.Arrays;
public class FinalExample... 阅读全帖
r*c
发帖数: 167
32
有人做了个, 没试过, 先贴上.
http://stackoverflow.com/questions/8635963/read-write-lock-impl
class rw_lock_t {
int NoOfReaders;
int NoOfWriters, NoOfWritersWaiting;
pthread_mutex_t class_mutex;
pthread_cond_t reader_gate;
pthread_cond_t writer_gate;
public:
rw_lock_t()
: NoOfReaders(0), NoOfWriters(0), NoOfWritersWating(0),
class_mutex(PTHREAD_MUTEX_INITIALIZER),
reader_gate(PTHREAD_COND_INITIALIZER),
writer_gate(PTHREAD_COND_INITIALIZER)
{}
~rw_lock_t... 阅读全帖
l*******0
发帖数: 63
33
想出两种思路,都是O(N)的time复杂度。请看注释。
public class HelloWorld{

//Idea:put every element into its right position. which means input[i]
is placed at input[i]-1. Then if input[i]==0, then i+1 is one missing
number.
//this approach modifies the original array.
//O(N) time
public static void printMissing(int[] input){
for(int i=0;i while(i+1 swap(input, i, input[i]-1);
}
... 阅读全帖
W***o
发帖数: 6519
34
这个是我CrimeTypeKey的class,生成composite key object:
public class CrimeTypeKey implements Writable, WritableComparable<
CrimeTypeKey>
{
private Text key = new Text();
private IntWritable year = new IntWritable();

public CrimeTypeKey() { }

public CrimeTypeKey(String k, int y)
{
key.set(k);
year.set(y);
}

public static CrimeTypeKey read(DataInput in) throws IOException
{
CrimeTypeKey keytp = new CrimeTypeKey();
keytp.readFields(... 阅读全帖
b********6
发帖数: 97
35
来自主题: JobHunting版 - 求leetcode LRU Java 解法
贴个我刚通过的解法 608ms
public class LRUCache {

static public class Pair {
int key;
int val;
Pair before;
Pair next;
public Pair(int k, int v){
key = k;
val = v;
before = null;
next = null;
}
}

//private LinkedList< Pair > item_list ;
//private ArrayDeque item_list;
private Pair head;
private Pair tail;
private HashMap item_map;
private int cap;
... 阅读全帖
l*********h
发帖数: 15
36
贴一个我的
public class LRUCache {
private Map map;
private Entry head;
private int size;
private final int CAPACITY;
private class Entry {
private int key;
private int val;
private Entry prev, next;
private Entry(){}
public Entry (int key, int val) {
this.key = key;
this.val = val;
}
}
public LRUCache(int capacity) {
map = new HashMap();
head = new Entry... 阅读全帖
n****e
发帖数: 678
37
来自主题: JobHunting版 - LinkedIn 面经
void memmov(void* src, void* dst, int numBytes) {
}
两位国人大哥一起面,自己对void pointer的概念有点忘了,这题也做得不好。
多谢国人大哥高抬贵手,让我过了。
bless一下自己后面的on-site。
l******6
发帖数: 340
38
struct node;
void print100(stack& waitStack);
struct childList{
node* curNode;
childList* next;
};
struct node{
void* data;
childList* children;
};
void print(node* root)
{
if(!root)
return;
childList* dummy = new childList();
dummy -> curNode = root;
dummy -> next = NULL;
stack waitStack;
waitStack.push(dummy);
while(!waitStack.empty())
{
print100(waitStack);
}
delete dummy;
}
void print1... 阅读全帖
Q*****a
发帖数: 33
39
来自主题: JobHunting版 - airBnb电面面经
实现了4种方法
1: 直接遍历完整计算edit distance. 285 clocks.
2: 直接遍历,计算edit distance到 >k 就返回. 149 clocks.
3: Trie+shortcut edit distance. Build trie: 606 clocks, process: 6 clocks.
http://stevehanov.ca/blog/index.php?id=114
4: Generate delete k transformation. Build dict: 17033 clocks. process: 0
clocks.
但这里不仅需要生成delete k的pattern, 还需要生成所有delete 1..k-1的pattern,
否则不能handle如(chrome brome)的case
http://blog.faroo.com/2012/06/07/improved-edit-distance-based-s
#include "common.h"
class Trie {
public:
class TrieNo... 阅读全帖
Q*****a
发帖数: 33
40
来自主题: JobHunting版 - airBnb电面面经
实现了4种方法
1: 直接遍历完整计算edit distance. 285 clocks.
2: 直接遍历,计算edit distance到 >k 就返回. 149 clocks.
3: Trie+shortcut edit distance. Build trie: 606 clocks, process: 6 clocks.
http://stevehanov.ca/blog/index.php?id=114
4: Generate delete k transformation. Build dict: 17033 clocks. process: 0
clocks.
但这里不仅需要生成delete k的pattern, 还需要生成所有delete 1..k-1的pattern,
否则不能handle如(chrome brome)的case
http://blog.faroo.com/2012/06/07/improved-edit-distance-based-s
#include "common.h"
class Trie {
public:
class TrieNo... 阅读全帖
S*******C
发帖数: 822
41
来自主题: JobHunting版 - Career cup 4.9 path sum的答案肯定错了
4.9 You are given a binary tree in which each node contains a value. Design
an algorithm to print all paths which sum to a given value. The path does
not need to start or end at the root or a leaf
Page 237
他的答案如下
package Question4_9;
import CtCILibrary.TreeNode;
public class Question {

public static void findSum(TreeNode node, int sum, int[] path, int level
) {
if (node == null) {
return;
}

/* Insert current node into path */
path[level... 阅读全帖
a**********0
发帖数: 422
42
来自主题: JobHunting版 - java concurrence 例子
经典的consumer和producer的例子
package jc;
import java.util.*;
public class ProCon {
// a class object to store the strings
static List myListOne = new ArrayList();

public void produce(){
synchronized(myListOne){
myListOne.add("firstNameOne");
// notice all waiting threads to stop waiting
myListOne.notifyAll();
System.out.println("Adding finished and the current myListOne.
size(): " + myListOne.size());
... 阅读全帖
a**********0
发帖数: 422
43
来自主题: JobHunting版 - java concurrence 例子
继续consumer和producer的例子
package jc;
import java.util.*;
//what about there are two things to be synchronized?
public class ProCon2nd {
// a class object to store the strings
static List myListOne = new ArrayList();
static List myListTwo = new ArrayList();
// this is the thing getting synchronized
// it is static as it is the same for all instances
static Object myLock = new Object();
public void produce(){
... 阅读全帖
p********r
发帖数: 66
44
来自主题: JobHunting版 - leetcode做伤心了
1. 从四个边是O的位置开始,广度优先搜索相邻的位置是O的,并且标记为R. 这一步用
时O(n^2)
2. 把所有是R的位置标记为O,所有是O的位置标记为X,用时O(n^2)
因为输入是二位矩阵所有,O(n^2)时间复杂度是最好的了。 Java 代码 572ms跑完所有
test case
具体代码如下:
public void solve(char[][] board) {
if(board.length < 3) return;
Stack stack = new Stack();

for(int i = 0; i < board.length; i++){
if(board[i][0] == 'O'){
insert(stack, i, 0);
}

if(board[i].length > 1 && board[i][board[i].length - 1... 阅读全帖
I**********n
发帖数: 77
45
来自主题: JobHunting版 - 请教如何实现图的数据结构C++
-------
美国CS面试工作交流群QQ: 167615205
--------
看下面的code
#include
#include
#include
#include
using namespace std;
struct vertex{
typedef pair ve;
vector adj; //cost of edge, destination vertex
string name;
vertex(string s)
{
name=s;
}
};
class graph
... 阅读全帖
m******g
发帖数: 100
46
来自主题: JobHunting版 - google 电面
My solution in Java. Permutation first then search
public class FindInDictionary {

private static int numOfPerm;
private static int index;
private static String [] allPerms;

private static void swap (char[] a, int i, int j){
char c = a[i]; a[i]=a[j]; a[j]=c;
}

//recursive way of creating permutation
private static void permutation (char[] a, int n)
{
if (n==1)
{
// System.out.println(String.valueOf(a));... 阅读全帖
f**********t
发帖数: 1001
47
来自主题: JobHunting版 - 热腾腾的 LinkedIn 电面题攒RP
// Given a binary tree where all the right nodes are either leaf nodes with
a sibling (a left node that shares the same parent node) or empty, flip it
upside down and turn it into a tree where the original right nodes turned
into left leaf nodes. Return the new root.
// {1,2,3,4,5}, to [4,5,2,#,#,3,1]
class BiTree {
struct Node {
char val;
Node *left, *right;
Node(char c) : val(c), left(nullptr), right(nullptr) {}
};
Node *_root;
public:
BiTree(string s) {
queue qu... 阅读全帖
a***e
发帖数: 413
48
最近几天的少得可怜的准备时间都在看这个。60多行程序改个地方就通不过了。区别仅
在于
while(1)
{
r.push_back(p->val);
if(p==from)
break;
p = p->right;
}
不能改成
while(p!=from)
{
r.push_back(p->val);
p = p->right;
}
哎,可能还是因为这个算法不是自己想出来的,总是糊涂。
偶尔说起来,别人说那种属于比较偏的题了,如果问道肯定是存心要fail某人。
就是好奇有没有人真被问到。
能通过OJ的,
void reverse(TreeNode *from, TreeNode *to)
{
if (from==to)
return;

TreeNode *... 阅读全帖
a***e
发帖数: 413
49
知道要O(1)space的得用Morris Traversal。但想先写个简单的,却发现改了好几次。
1。 开始没想清楚怎么把那两个错误的node找出来
2. 应该用*& 的地方用了*。 后来发现不作为param来pass还更简单。
但这样也有很多行,等有时间写个Morris的比较下。感觉也不是那么简单啊,sigh!
class Solution {
public:
TreeNode *first, *second, *pre;
void helper(TreeNode *cur)
{
if (cur==NULL)
return;

helper(cur->left);
if (pre)
{
if (pre->val>cur->val)
{
if (first==NULL)
{
first = pre;
sec... 阅读全帖
a***e
发帖数: 413
50
嗯,试图写O(1)的,发现用vector把指针装起来,再pass reference容易一些。但是
不知道为啥初始化的时候下面这个不行
vector m(2, NULL);
Compile Error
required from ‘std::vector<_Tp, _Alloc>::vector(_InputIterator, _
InputIterator, const allocator_type&) [with _InputIterator = int; _Tp =
TreeNode*; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::
allocator_type = std::allocator]’
得换成这几行
vector m;
m.push_back(NULL);
m.push_back(NULL);
还没仔细debug 为什么下面这个答案不对,而后面那个就对。
错... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 (共10页)