思路:
先全部入栈。然后依次出栈,和原始对比,是否相同
链栈-数字回文
bool isPalindrome(ListNode* head) {
stack<int>st;
ListNode* cur = head;
while (cur)
{
st.push(cur->val);
cur = cur->next;
}
while (head)
{
if (head->val != st.top())
{
return false;
}
head = head->next;
st.pop();
}
return true;
}
顺序栈-字母回文
bool judge(string str) {
// write code here
stack<char>st;
for (auto c : str)
{
st.push(c);
}
for (auto c : str)
{
if (c != st.top())
{
return false;
}
st.pop();
}
return true;
}