判断是否是回文结构

判断是否是回文结构

题目如下
在这里插入图片描述

我的思路是分三种情况

  1. 先判断最左与最右是否相等,如果相等则继续判断剩下的元素,不相等直接输出answer=false
  2. 剩下两种,是把这个链表里的数组进行差分。差分情况分奇、偶数。但检测的思路都是一样的,就是把前半部分的元素放到一个栈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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值