LeetCode 125 Valid Palindrome
2015-11-30 09:21
363 查看
题目描述
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.
分析
注意忽略非字母数字的字符代码
[code] public static boolean isPalindrome(String s) { if (s.length() <= 1) { return true; } char[] chars = s.toLowerCase().toCharArray(); for (int st = 0, ed = chars.length - 1; st <= ed; st++, ed--) { while (st < ed && !isValid(s, st)) st++; while (st < ed && !isValid(s, ed)) ed--; if (chars[st] != chars[ed]) return false; } return true; } static boolean isValid(String s, int i) { char c = s.charAt(i); return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')|| (c >= 'A' && c <= 'Z'); }
相关文章推荐
- 数据抓取之反爬虫规则:使用代理和http头信息
- [LeetCode]Different Ways to Add Parentheses
- PHP curl模拟登录带验证码的网站
- bootstrap中的段落和强调内容
- MapReduce InputFormat之FileInputFormat
- 浏览博客随笔——handler正确使用
- jquery中attr和prop的区别
- WPF自定义控件与样式(11)-等待/忙/正在加载状态-控件实现
- Android SparseArray ArrayMap
- Backbone React Requirejs 应用实战(一)——RequireJS管理React依赖
- Winform 保持某窗体在最上层
- 数据抓取之反爬虫规则:验证码识别
- marquee语法
- 【Java并发编程实战】-----“J.U.C”:Exchanger
- hdu2036 改革春风吹满地 (求多边形面积,叉积)
- hdu计算机学院大学生程序设计竞赛(2015’11)1001 搬砖
- 《利用python进行数据分析》读书笔记--第五章 pandas入门
- 按位与、或、异或等运算方法
- (NO.00004)iOS实现打砖块游戏(十一):"一闪一闪亮晶晶,我们都是小星星"
- (NO.00004)iOS实现打砖块游戏(十一):"一闪一闪亮晶晶,我们都是小星星"