博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode OJ - Valid Palindrome
阅读量:7076 次
发布时间:2019-06-28

本文共 1098 字,大约阅读时间需要 3 分钟。

这道题挺简单的,但是需要细心。

最好的方法是先对string做预处理,然后再判断是否是回文。

下面是AC代码:

1 /** 2      * Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. 3      * @param s 4      * @return 5      */ 6     public boolean isPalindrome(String s){ 7         String pre = preProcess(s); 8         char[] sw = pre.toCharArray(); 9         if(pre==null || pre.length()<=1 )10             return true;11         int i=0;12         int j=pre.length()-1;13         while(i<=j){14             if(sw[i]!=sw[j])15                 return false;16             i++;17             j--;18         }19         return true;20     }21     /**22      * pre-processing the string, remove all non-isAlphanumeric23      * and change all to lower24      * @param s25      * @return26      */27     private String preProcess(String s){28         29         StringBuffer sb = new StringBuffer();30         for(int i=0;i
='a' && c<='z' || c>='A' && c<='Z' || c>='0'&& c<='9')42 return true;43 return false;44 }

 

转载于:https://www.cnblogs.com/echoht/p/3701100.html

你可能感兴趣的文章
创建本地CM 离线服务器
查看>>
PHP数组操作——取数组最后一个值
查看>>
springboot集成swagger2
查看>>
node.js学习之流解析(一)
查看>>
YxdIOCP (DIOCP修改版)
查看>>
转:进程 线程 协程 管程 纤程 概念对比理解
查看>>
站内全文搜索
查看>>
scala函数和方法的差别
查看>>
苹果平台上的媒体流播放技术HLS
查看>>
图书馆管理系统程序设计
查看>>
WebService Rest接收大量数据出现基础连接已经关闭的解决方案
查看>>
小R的烦恼 BZOJ3280
查看>>
左神算法基础班4_5折纸问题
查看>>
【整理】SYSCOMMAND的wParam值的宏定义
查看>>
.net Application的目录
查看>>
洛谷 P1313 计算系数 Label:杨辉三角形 多项式计算
查看>>
YUV色彩空间(转自百度百科)
查看>>
创建服务
查看>>
Sencha Touch 2.1学习图表Chart概述
查看>>
NYOJ467 中缀式变后缀式
查看>>