验证是否回文
来源:互联网 发布:介绍人工智能的小短片 编辑:程序博客网 时间:2024/06/06 14:02
把相同的词汇或句子,调换位置或颠倒过来,产生首尾回环叫做回文。
如“abba”、“abccba”、12321、123321是“回文”,“abcde”和“ababab”则不是“回文”。
以下核心代码help(s)的效率为O(m)
public class Palindrome {
public static void main(String[] args) {
String s = "A man, a plan, a canal: Panama";
boolean b = isPalindrome(s);
System.out.println(b);
}
public static String tool(String s ){
s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
return s;
}
public static boolean isPalindrome(String s){
s = tool(s);
return help(s);
}
private static boolean help(String s){
int len = s.length();
int index = 0;
int m = len / 2;
Stack<Character> stack = new Stack<Character>();
while(index < m){
stack.push(s.charAt(index));
index++;
}
if(len % 2 == 1){
index++;
}
while(index < len){
if(stack.empty()){
return false;
}
char a = stack.pop();
if(a != s.charAt(index)){
return false;
}else{
index++;
}
}
return true;
}
}
- 验证是否回文
- Valid Palindrome:验证是否为回文结构
- Java程序名验证是否是回文字符
- leetcode-125. Valid Palindrome(验证含有标点符号String是否为回文串)
- 判断是否为回文
- 判断字符串是否回文
- 判断字符串是否回文
- 判断是否是回文
- 判断是否为回文
- 判断字符串是否回文
- 数字 是否是回文
- 判断是否回文数
- 判断是否回文数
- java判断是否回文
- 算法题:是否回文
- 判断是否为回文
- 判断是否为回文
- 判断字符串是否回文
- Button响应鼠标点击事件
- 带按钮Listview适配器
- leetcode第一刷_Merge Two Sorted Lists
- 敏捷软件开发原则模式与实践语录参考
- 在.NET中调用DataWindow操作数据库
- 验证是否回文
- Correct Method of Changing the IP Address in 11i & R12 (Doc ID 751328.1)
- ams增加用户验证,实现开机自动推流。
- servlet--过滤器的使用
- hdu 3336 Count the string (DP+kmp)
- 公司评论
- 【Android】4.04系统WebView播放不了Flash问题的解决办法
- 关于指针的一些知识点
- 设置状态栏字体颜色