有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java中使用堆栈和队列确定回文

在这里,我试图通过使用堆栈和队列来确定一个单词或短语是否是回文,这取决于我所写的短语

它所做的是,它说所有的东西都是回文,并根据它有多少个字母来写“回文”

我猜我需要在最后一个for循环和while循环之间添加一些内容,但我不确定是什么

public class CheckPalindrome {

public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    while (true) {

        String line = reader.readLine();


        if (line.toLowerCase().equals("quit")) {
            break;
        }

        Stack<Character> stack = new Stack<Character>();

        Queue<Character> queue = new LinkedList<Character>();

         for (int i = 0; i < line.length(); i++) {
                stack.push(line.charAt(i));
            }

         for (int i = line.length() - 1; i >= 0; i--) {
             queue.add(line.charAt(i));
         }

          while (!queue.isEmpty()) {
             if (queue.remove().equals(stack.pop())) {
                 System.out.println("Palindrome");
             } else {
                 System.out.println("Not a Palindrome");
             }
         }

      }     
   }
}

共 (5) 个答案

  1. # 1 楼答案

    我对您的代码做了一些非常小的修改(首先是修复一个for循环,其次是防止“回文/非回文”消息对输入中的每个字符打印一次),以使其正常工作:

    import java.util.Stack;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.io.*;
    class Palindrome {
        public static void main(String[] args) throws IOException {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    
            while (true) {
    
                String line = reader.readLine();
    
    
                if (line.toLowerCase().equals("quit")) {
                    break;
                }
    
                Stack<Character> stack = new Stack<Character>();
    
                Queue<Character> queue = new LinkedList<Character>();
    
                for (int i = 0; i < line.length(); i++) {
                    stack.push(line.charAt(i));
                }
    
                for (int i = 0; i < line.length(); i++) {
                    queue.add(line.charAt(i));
                }
    
                boolean isPalindrome=true;
                while (!queue.isEmpty()) {
                    if (queue.remove().equals(stack.pop())) {
                        continue;
                    } else {
                        isPalindrome=false;
                        break;
                    }
                }
                if (!isPalindrome) {
                    System.out.println("Not a Palindrome");
                } else {
                    System.out.println("Palindrome");
                }
    
            }     
        }
    }
    
  2. # 2 楼答案

    您需要以相同的顺序将字符放入堆栈和队列中的每个部分。使用两者的意义在于,一个颠倒顺序,另一个不颠倒顺序。你自己在其中一个上面颠倒顺序,就像你现在所做的那样,否定了这一点

  3. # 3 楼答案

    这是一个新的解决方案。试试这个。如果有任何修改,请告诉我。 包栈

    public class pallindrome {
    
    Stack<Integer> stack = new Stack<Integer>();
    Queue<Integer> queue = new LinkedList<Integer>();
    
    void pushh(int new_Data) {
        stack.push(new_Data);
    }
    
    void enquee(int new_Data) {
    
        queue.add(new_Data);
    }
    
    int popStack(){
        return stack.pop();
    }
    
    int dequeueQueue() {
        return queue.remove();
    }   
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            String input = scan.nextLine();
            scan.close();
    
            // Convert input String to an array of characters:
            char[] s = input.toCharArray();
    
            // Create a Solution object:
            pallindrome p = new pallindrome();
    
            // Enqueue/Push all Integer to their respective data structures:
            for(int i=0;i<input.length();i++)
            {
                p.pushh(i);
                p.enquee(i);
            }
            // Pop/Dequeue the chars at the head of both data structures and compare them:
            boolean isPalindrome = true;
            for ( i = 0; i < s.length/2; i++) {
                if (p.popStack() != p.dequeueQueue()) {
                    isPalindrome = false;                
                    break;
                }
            }
    
            //Finally, print whether string s is palindrome or not.
            System.out.println( "The Integer, " + input + ", is " 
                               + ( (!isPalindrome) ? "not a palindrome." : "a palindrome." ) );
        }
    }
    
  4. # 4 楼答案

    在JAVA中同时使用堆栈和队列检查回文

        public class Palindrome {
    
    
        public static void main(String[] args) {
    
            Scanner input = new Scanner(System.in);
    
            System.out.println("Enter the String:: ");
            String real = input.nextLine();
    
            Queue q = new LinkedList();
            Stack st = new Stack();
    
    
            for(int i=0; i<=real.length()-1; i++) {
                q.add(real.charAt(i));
            }
    
            for(int i=0; i<real.length(); i++) {
                st.push(real.charAt(i));
            }
    
            if(q.remove().equals(st.pop())) {
                System.out.println("Palindrom");
            }else {
                System.out.println("Not Palindrom");
            }
    
        }
    
    }
    
  5. # 5 楼答案

    有趣的是,这里有一个使用^{}的方法的变体,而不是单独使用堆栈和队列。Deque只是一个双端队列(即同时作为两个队列运行)

    public static boolean isPalindrome(String word) {
        boolean isPalindrome = word.length() == 1;
        if (!isPalindrome) {
            Deque<Character> wordDeque = new LinkedList<>();
            for (Character c : word.toCharArray()) {
                wordDeque.add(Character.toLowerCase(c));
            }
            isPalindrome = true;
            while (isPalindrome && wordDeque.size() > 1) {
                isPalindrome = wordDeque.pollFirst().compareTo(wordDeque.pollLast()) == 0;
            }
        }
        return isPalindrome;
    }