判断是否是回文结构
题目如下
我的思路是分三种情况
- 先判断最左与最右是否相等,如果相等则继续判断剩下的元素,不相等直接输出answer=false
- 剩下两种,是把这个链表里的数组进行差分。差分情况分奇、偶数。但检测的思路都是一样的,就是把前半部分的元素放到一个栈stack1里面,再把后半部分的元素放到stack2里面,stack2的元素再弹出放到stack3里,这样它元素的先后顺序就能和stack1一样。然后依次判断stack1和stack3弹出的元素是否相等。如果出现不相等的情况就可以退出判断,如果一直相等,那它就是回文结构
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
int b=0;
int c=0;
int d=0;
Stack<Integer> stack1=new Stack<Integer>();
Stack<Integer> stack2=new Stack<Integer>();
Stack<Integer> stack3=new Stack<Integer>();
ListNode a=head;
ListNode h=head;
boolean answer=true;
while(h!=null)
{
b=h.val;
h=h.next;
c=c+1;
}
if(b!=head.val)
{
answer=false;
}
else if(c%2==0)
{
c=c/2;
for(int i=0;i<c;i++)
{
stack1.push(a.val);
a=a.next;
}
for(int i=0;i<c;i++)
{
stack2.push(a.val);
a=a.next;
}
for(int i=0;i<c;i++)
{
stack3.push(stack2.pop());
}
for(int i=0;i<c;i++)
{
if(stack1.pop()!=stack3.pop())
{
answer=false;
break;
}
answer=true;
}
}
else if(c%2!=0)
{
c=c/2;
for(int i=0;i<c;i++)
{
stack1.push(a.val);
a=a.next;
}
a=a.next;
for(int i=0;i<c;i++)
{
stack2.push(a.val);
a=a.next;
}
for(int i=0;i<c;i++)
{
stack3.push(stack2.pop());
}
for(int i=0;i<c;i++)
{
if(stack1.pop()!=stack3.pop())
{
answer=false;
break;
}
answer=true;
}
}
return answer;
}
}
然后得到的结果是还有两组数据输出的答案是错误的
应该是判断false里面出了问题,判断的pop对应不上出错了
然后我在pop之前加了一个判断,该栈是否为空,依旧是有错
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
int b=0;
int c=0;
int d=0;
Stack<Integer> stack1=new Stack<Integer>();
Stack<Integer> stack2=new Stack<Integer>();
Stack<Integer> stack3=new Stack<Integer>();
ListNode a=head;
ListNode h=head;
boolean answer=true;
while(h!=null)
{
b=h.val;
h=h.next;
c=c+1;
}
if(b!=head.val)
{
answer=false;
}
else if(c%2==0)
{
c=c/2;
for(int i=0;i<c;i++)
{
stack1.push(a.val);
a=a.next;
}
for(int i=0;i<c;i++)
{
stack2.push(a.val);
a=a.next;
}
for(int i=0;i<c;i++)
{
stack3.push(stack2.pop());
}
for(int i=0;i<c;i++)
{
if(stack1.peek()!=null&&stack3.peek()!=null){
if(stack1.pop()!=stack3.pop())
{
answer=false;
break;
}
}
// answer=true;
}
}
else if(c%2!=0)
{
c=c/2;
for(int i=0;i<c;i++)
{
stack1.push(a.val);
a=a.next;
}
a=a.next;
for(int i=0;i<c;i++)
{
stack2.push(a.val);
a=a.next;
}
for(int i=0;i<c;i++)
{
stack3.push(stack2.pop());
}
for(int i=0;i<c;i++)
{
if(stack1.peek()!=null&&stack3.peek()!=null){
if(stack1.pop()!=stack3.pop())
{
answer=false;
break;
}
}
// answer=true;
}
}
return answer;
}
}
然后看了很久我又改了一个地方,我发现那个变量c
c=c/2;
这里第一个else if会改变c的值,然后导致它有可能执行完第一个else if继续执行第二个else if 语句。
但改完以后还是有问题!
最后我不断地查看,我发现这句话特别奇怪
stack1.pop()!=stack3.pop()
然后把它改了,先进行pop的赋值再进行比较
q=stack1.pop();
w=stack3.pop();
if(q!=w)
{
answer=false;
break;
}
具体为什么不能直接比较pop的数值还要查一下
最后终于可以得到结果是(虽然运行效率几乎垫底,但也算是通过了),日后再看看有什么办法简化程序
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
int b=0;
int c=0;
int d=0;
int q=0;
int w=0;
Stack<Integer> stack1=new Stack<Integer>();
Stack<Integer> stack2=new Stack<Integer>();
Stack<Integer> stack3=new Stack<Integer>();
ListNode a=head;
ListNode h=head;
boolean answer=true;
while(h!=null)
{
b=h.val;
h=h.next;
c=c+1;
}
if(b!=head.val)
{
answer=false;
}
else if(c%2==0)
{
//这里面c的值改变了,就会执行下一个函数c的值
d=c/2;
for(int i=0;i<d;i++)
{
stack1.push(a.val);
a=a.next;
}
for(int i=0;i<d;i++)
{
stack2.push(a.val);
a=a.next;
}
for(int i=0;i<d;i++)
{
stack3.push(stack2.pop());
}
for(int i=0;i<d;i++)
{
// if(stack1.peek()!=null&&stack3.peek()!=null){
q=stack1.pop();
w=stack3.pop();
if(q!=w)
{
answer=false;
break;
}
// }
// answer=true;
}
}
else if(c%2!=0)
{
c=c/2;
for(int i=0;i<c;i++)
{
stack1.push(a.val);
a=a.next;
}
a=a.next;
for(int i=0;i<c;i++)
{
stack2.push(a.val);
a=a.next;
}
for(int i=0;i<c;i++)
{
stack3.push(stack2.pop());
}
for(int i=0;i<c;i++)
{
// if(stack1.peek()!=null&&stack3.peek()!=null){
q=stack1.pop();
w=stack3.pop();
if(q!=w)
{
answer=false;
break;
}
// }
// answer=true;
}
}
return answer;
}
}