Re: [问题] Google Interview Question (1)

楼主: aceldama (2-4)   2013-05-12 04:14:43
※ 引述《pnpncat (meow)》之铭言:
: ※ 引述《RockLee (Now of all times)》之铭言:
: 1. 将A, B, C分别放进stackA, stackB, stackC
: 2. 以下列函数判别答案是否为真
: bool foo() {
: while ( stackC is not empty ) {
: if ( top of stackC == top of stackA ) {
: pop stackC;
: pop stackA;
: } else if (top of stackC == top of stackB ) {
: pop stackC;
: pop stackB;
: } else {
: return false;
: }
: }
: return true;
: }
我觉得stack 可行 问题就是要解决怎么re trace
stack tempA
while C not empty
if (top of stackA = top of stackB = top of stackC){
pop C
top of A add to end of tempA
pop A
} else if (top of stackA = top of stackC){
if(tempA is not empty){
top of A add to tempA
}
pop A
pop C
} else if (top of B = top of C){
if (tempA is not empty){
if (top of C = top of tempA){
pop tempA
} else return false
}
pop B
pop C
} else {
var temp = pop tempA
if(temp = top of B){
push (A, temp)
pop B
temp = pop B
if (top of C != temp) return false
pop C
} else return false
}
}

Links booklink

Contact Us: admin [ a t ] ucptt.com