第三题
# include <iostream>
using namespace std;
bool solution ( char s[ ] ) {
int n= 8 ;
int numI= 0 ;
for ( int i= 0 ; i< n; i++ ) {
if ( s[ i] == 'I' ) {
numI++ ;
}
if ( s[ i] == 'O' ) {
if ( numI== 0 ) {
return false ;
}
numI-- ;
}
}
return true ;
}
int main ( ) {
char s[ 8 ] = { 'I' , 'O' , 'O' , 'I' , 'O' , 'I' , 'I' , 'O' } ;
if ( solution ( s) ) {
cout<< "true" ;
} else {
cout<< "false" ;
}
}
第四题
# include <iostream>
# include <stack>
using namespace std;
struct listNode {
char val;
struct listNode * next;
} ;
bool reList ( listNode* & head, int n) {
int i;
char s[ n/ 2 ] ;
listNode * p= head;
for ( i= 0 ; i< n/ 2 ; i++ ) {
s[ i] = p-> val;
p= p-> next;
}
i-- ;
if ( n% 2 == 1 ) {
p= p-> next;
}
while ( p!= NULL && s[ i] == p-> val) {
i-- ;
p= p-> next;
}
if ( i== - 1 ) {
return true ;
} else {
return false ;
}
}
int main ( ) {
listNode node5= { 'x' , nullptr } ;
listNode node4= { 'y' , & node5} ;
listNode node3= { 'y' , & node4} ;
listNode node2= { 'y' , & node3} ;
listNode node1= { 'x' , & node2} ;
listNode * L= & node1;
if ( reList ( L, 5 ) ) {
cout<< "true" ;
} else {
cout<< "false" ;
}
}
第五题
# include <iostream>
using namespace std;
# define Max 50
typedef struct {
int data[ Max] ;
int top[ 2 ] ;
} stack1;
stack1 s;
int push ( int i, int x) {
if ( i!= 0 && i!= 1 ) {
cout<< "栈号不对" << endl;
return - 1 ;
}
if ( s. top[ 1 ] - s. top[ 0 ] == 1 ) {
cout<< "栈满" << endl;
return - 1 ;
}
if ( i== 0 ) {
s. data[ ++ s. top[ 0 ] ] = x;
}
else {
s. data[ -- s. top[ 1 ] ] = x;
}
return 1 ;
}
int pop ( int i) {
if ( i!= 0 && i!= 1 ) {
cout<< "栈号不对" ;
}
if ( i== 0 ) {
if ( s. top[ 0 ] == - 1 ) {
cout<< "0号栈为空" << endl;
return - 1 ;
}
return s. data[ s. top[ 0 ] -- ] ;
} else {
if ( s. top[ 1 ] == Max) {
cout<< "1号栈栈空" ;
return - 1 ;
}
return s. data[ s. top[ 0 ] ++ ] ;
}
}
int main ( ) {
s. top[ 0 ] = - 1 , s. top[ 1 ] = Max;
if ( push ( 0 , 1 ) != - 1 ) {
cout<< "0号栈进栈成功" << endl;
}
int x= pop ( 0 ) ;
if ( x!= - 1 ) {
cout<< "0号栈出栈成功" ;
cout<< "0号栈出栈元素为:" << x<< endl;
}
int y= pop ( 0 ) ;
return 0 ;
}