LeetCode——Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.
Note:
Have you consider that the string might be empty?
This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
原题链接:https://oj.leetcode.com/problems/valid-palindrome/
题目:给定一个字符串,检測其是否是回文串,仅仅考虑字母数字字符。
思路:过滤源字符串中的非字母数字,组成一个新串,对新串进行推断。
public static boolean isPalindrome(String s) {
if(s.isEmpty())
return true;
//过滤字母数字之外的字符
StringBuffer buf = new StringBuffer();
for(int i=0;i<s.length();i++){
if(Character.isLetterOrDigit(s.charAt(i)))
buf.append(s.charAt(i));
}
String tmp = buf.toString().toLowerCase();
for(int i=0;i<tmp.length();i++){
if(tmp.charAt(i) != tmp.charAt(tmp.length() - i - 1))
return false;
}
return true;
}
LeetCode——Valid Palindrome的更多相关文章
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] Valid Palindrome II 验证回文字符串之二
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- [leetcode]Valid Palindrome @ Python
原题地址:https://oj.leetcode.com/problems/valid-palindrome/ 题意: Given a string, determine if it is a pal ...
- LeetCode Valid Palindrome II
原题链接在这里:https://leetcode.com/problems/valid-palindrome-ii/description/ 题目: Given a non-empty string ...
- Leetcode Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode: Valid Palindrome [125]
[题目] Given a string, determine if it is a palindrome, considering only alphanumeric characters and i ...
- LeetCode: Valid Palindrome 解题报告
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- [Leetcode] valid palindrome 验证回文
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- leetcode Valid Palindrome C++&python 题解
题目描写叙述 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
随机推荐
- Windows上帝模式
在桌面新建一个文件夹 按F2重命名为 GodMode.{ED7BA470-8E54-465E-825C-99712043E01C} 完成
- eclipse设置和优化
1.eclipse下的编码设置: eclipse 中使用模板新建 JSP,xhtml等 文件时,默认的编码为:ISO-8859-1. ISO-8859-1 编码对于中文的显示是不支持的,如果要支持简体 ...
- socket.io实现
后台代码 index_server.js var app = require('http').createServer(handler)//创建服务器app , io = require('socke ...
- Delphi 动态创建组件,单个创建、单个销毁
效果图如下: 实现部分代码如下: var rec: Integer = 0; //记录增行按钮点击次数 implementation {$R *.dfm} //动态释放单个组件内存,即销毁组件 pro ...
- Android单位度量
px(像素):屏幕上的点. in(英寸):长度单位.mm(毫米):长度单位.pt(磅):1/72英寸.dp(与密度无关的像素):一种基于屏幕密度的抽象单位.在每英寸160点的显示器上,1dp = 1p ...
- BroadcastReceiver监听电量变化
用BroadcastReceiver监听电量的变化,可以实现BroadcastReceiver接收电量变化的广播,然后获取电量百分比信息. BatteryChangedReceiver.java pu ...
- element的height与width
关于一个element所有的高度宽度 ele.style.width,ele.style.height:操纵style样式.+"px" offsetWidth.offsetHeig ...
- md5实现
/** * @param str * @return */ public static String stringToMD5(String str, String encode) { try { by ...
- centos 下搭建 php环境(1)
3.PHP的安装 安装GD库(让PHP支持GIF,PNG,JPEG) 首先下载 jpeg6,libpng,freetype 并安装模块 wget http://www.ijg.org/files/jp ...
- Centos 6安装python3.5
安装python3.5 安装步骤如下 :1 准备编译环境(环境如果不对的话,可能遇到各种问题,比如wget无法下载https链接的文件) yum groupinstall 'Development T ...