除了使用抽象数据类型“STACK”之外,还有其他方法来查找回文吗?

发布于 2024-10-13 04:10:51 字数 1862 浏览 2 评论 0原文

除了使用堆栈数据结构之外,我想要任何其他有效的方法来查找字符串的回文。这是我使用堆栈操作编写的代码。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package StringRevUsingStack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class StringReverseThroughStack {
    // private data members;
    private String InputStr;
    private String OutputStr;

    //Constructor
    public StringReverseThroughStack(String ip){
        InputStr = ip;
    }

    public String doReverse(){
        Stack theStack = new Stack(InputStr.length());
        String revStr;
        for(int i=0;i<InputStr.length();i++)
        {
            theStack.push(InputStr.charAt(i));
        }
        revStr="";
        while(!theStack.isEmpty()){
            revStr+=theStack.pop();
        }
        return revStr;
    }

    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the String:");
        String ip=br.readLine();
        StringReverseThroughStack theStr = new StringReverseThroughStack(ip);
        String op=theStr.doReverse();
        if(ip.compareTo(op)==0)
            System.out.println("It is a palindrome");
        else
            System.out.println("It is a not palindrome");

    }

}
class Stack{
    private int maxSize;
    private char[] stackArray;
    private int Top;

    public Stack(int max){
        maxSize = max;
        stackArray = new char[maxSize];
        Top=-1;
    }
    public void push(char item){
        stackArray[++Top]=item;
    }

    public char pop(){
        return stackArray[Top--];
    }

    public char peek(){
        return stackArray[Top];
    }

    public boolean isEmpty(){
   return (Top == -1);
    }

}

i want to any other efficient way to find the palindrome of string other than using stack data structure. this is code i have written using stack operation.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package StringRevUsingStack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class StringReverseThroughStack {
    // private data members;
    private String InputStr;
    private String OutputStr;

    //Constructor
    public StringReverseThroughStack(String ip){
        InputStr = ip;
    }

    public String doReverse(){
        Stack theStack = new Stack(InputStr.length());
        String revStr;
        for(int i=0;i<InputStr.length();i++)
        {
            theStack.push(InputStr.charAt(i));
        }
        revStr="";
        while(!theStack.isEmpty()){
            revStr+=theStack.pop();
        }
        return revStr;
    }

    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the String:");
        String ip=br.readLine();
        StringReverseThroughStack theStr = new StringReverseThroughStack(ip);
        String op=theStr.doReverse();
        if(ip.compareTo(op)==0)
            System.out.println("It is a palindrome");
        else
            System.out.println("It is a not palindrome");

    }

}
class Stack{
    private int maxSize;
    private char[] stackArray;
    private int Top;

    public Stack(int max){
        maxSize = max;
        stackArray = new char[maxSize];
        Top=-1;
    }
    public void push(char item){
        stackArray[++Top]=item;
    }

    public char pop(){
        return stackArray[Top--];
    }

    public char peek(){
        return stackArray[Top];
    }

    public boolean isEmpty(){
   return (Top == -1);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2024-10-20 04:10:51

StringBuffer 有一个 reverse() 方法。这应该就是您所需要的。

StringBuffer has a reverse() method. That should be all you need.

殤城〤 2024-10-20 04:10:51

您只需使用一个简单的 for 循环反向迭代字符串即可做到这一点。

(假设是java)

public String ReverseThisString(String inputStr) {
    String outputStr = new String();

    for(int i = inputStr.length() - 1; i >= 0; --i) {
        outputStr += String.valueOf(inputStr.charAt(i));
    }

    return outputStr;
}

You could do this just with a simple for loop iterating over the string in reverse.

(Assuming java)

public String ReverseThisString(String inputStr) {
    String outputStr = new String();

    for(int i = inputStr.length() - 1; i >= 0; --i) {
        outputStr += String.valueOf(inputStr.charAt(i));
    }

    return outputStr;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文