topic:
analysis:
Purple done on the book, not the position x, x the first top turn and turn the specified location.
Code:
vector<int> A;
vector<int> v;
if(A.size()==1) return v;
if(A.size()==2)
{
if(A[0]==1) return v;
v.push_back(2);
return v;
}
for(int i=A.size();i>1;i--)
{
if(A[i-1]==i) continue;
int loca;
//找位置
for(int j=i-1;;j--)
{
if(A[j-1]==i)
{
loca=j-1;break;
}
}
//反转到最下面
if(loca!=0)
{
v.push_back(loca+1);
stack<int> s;
for(int j=loca;j>=0;j--) s.push(A[j]);
for(int j=loca;j>=0;j--) {
A[j]=s.top();
s.pop();
}
}
//反转到指定位置 i-1
stack<int> s;
for(int j=i-1;j>=0;j--) s.push(A[j]);
for(int j=i-1;j>=0;j--) {
A[j]=s.top();
s.pop();
}
v.push_back(i);
}
if(A[0]!=1) v.push_back(2);
return v;