leetcode的第一题,回文数判断。

原题如下:

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.

回文数是个很基础的东西,这里给了两个example作为提示:给的样本中可能会有空格,标点和特殊符号等,要考虑一下先判断string是否是纯string再去进行回文判断。

leetcode给的答案是伪代码,也解释的很清楚:

public boolean isPalindrome(String s)
{
int i = , j = s.length() - ; while (i < j)
{ while (i < j && !Character.isLetterOrDigit(s.charAt(i))) i++;
while (i < j && !Character.isLetterOrDigit(s.charAt(j))) j--; if (Character.toLowerCase(s.charAt(i))
!= Character.toLowerCase(s.charAt(j)))
{
return false;
} i++; j--; } return true; }

思路结束,具体在操作的时候就是C++的能力本身了。随手写的代码(日后改)[2014-11-09]:

#include <string>
#include <algorithm> bool isPalindrome(string s)
{
if (s.empty())
{
return false;
}
string tmp = "";
for (int i = ; i < s.size(); i++)
{
if (isalnum(s.at(i)))
{
tmp += s[i];
}
}
transform(tmp.begin(), tmp.end(), tmp.begin(), ::toupper);
for (int i = ; i < tmp.size() / ; i++)
{
if (tmp.at(i) != tmp.at(tmp.size() - - i))
{
return false;
}
} return true;
}

C++水平还不够,但是过了。回文的问题以后再补充。

最新文章

  1. Hibernate注解映射sequence时出现无序增长问题+hibernate 映射 oracle ID自动增长:
  2. 2016.7.7 计算机网络复习要点第四章之网际协议IP
  3. 省份+城市---Dropdownlist控件的应用
  4. CSS笔记(十)position属性与定位
  5. 【POJ】【3710】Christmas Game
  6. css设置水平垂直居中
  7. CSS3学习笔记之linear-gradient
  8. RTL-SDR简单介绍
  9. 一款PHP环境整合工具—VertrigoServ介绍
  10. ajax中xmlhttp.readyState和xmlhttp.status的值及解释
  11. nginx limit_rate突然限速失败
  12. activiti 选人的实现
  13. MyBatis第一个案例的优化,通过映射文件与接口进行绑定
  14. SEO三种职位类型:编辑型SEO、技术型SEO、营销型SEO详解
  15. commit 流程
  16. 201621123008 《Java程序设计》第12周学习总结
  17. 大数据入门第二十二天——spark(三)自定义分区、排序与查找
  18. POJ 2823 Sliding Window(单调队列 || 线段树)题解
  19. plsql 执行批量文件
  20. 关于jQuery获得表单radio类型输入框的选中值

热门文章

  1. Oracle 创建表空间借鉴 保留,占版权留言告知
  2. 《Apache Velocity用户指南》官方文档
  3. 使用Vagrant创建多节点虚拟机集群
  4. Redis实战——redis主从备份和哨兵模式实践
  5. error C2039: &#39;SetDefaultDllDirectories&#39;错误解决办法&lt;转&gt;
  6. 网卡流量监控脚本 ( Python )
  7. 通过Curl 对url进行encode操作
  8. 通过maven自动修改idea的compiler
  9. jQuery代码在移动端不运行
  10. 日志管理,springboot