除了使用抽象数据类型“STACK”之外,还有其他方法来查找回文吗?
除了使用堆栈数据结构之外,我想要任何其他有效的方法来查找字符串的回文。这是我使用堆栈操作编写的代码。
/*
* 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
StringBuffer
有一个reverse()
方法。这应该就是您所需要的。StringBuffer
has areverse()
method. That should be all you need.您只需使用一个简单的 for 循环反向迭代字符串即可做到这一点。
(假设是java)
You could do this just with a simple for loop iterating over the string in reverse.
(Assuming java)