[leetcode] 1. Valid Palindrome
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++水平还不够,但是过了。回文的问题以后再补充。
[leetcode] 1. Valid Palindrome的更多相关文章
- [LeetCode] 680. Valid Palindrome II 验证回文字符串 II
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 1216. Valid Palindrome III
原题链接在这里:https://leetcode.com/problems/valid-palindrome-iii/ 题目: Given a string s and an integer k, f ...
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- 【题解】【字符串】【Leetcode】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- leetcode 125. Valid Palindrome ----- java
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 125. Valid Palindrome
这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...
- leetcode:Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- Android中SharePreferences的简单实现
Android中提供SharePreferences这种轻量级的数据存储模式,这种模式能够存储少量数据,并能为自身和其他应用提供数据接口.相对于其他数据存储方式,SharePreferences更加轻 ...
- 如何查找EI 及SCI 索引
1.EI https://www.engineeringvillage.com/search/quick.url 搜题目,然后点击搜索,然后点击细节(detailed) Accession numbe ...
- Bootstrap 与 Jquery validate 结合使用——多个规则实现
进行开发的时候,遇到了需要有多个规则来校验,如新用户过来一套校验规则,老用户过来又是一套规则,这时候就要需要定义多套校验规则. 首先要熟悉Bootstrap和Jquery validate的使用,详情 ...
- \\IP\e$方式访问服务器的E盘被拒绝是什么原因?
问题:服务器本地管理员的权限,首先访问是被拒绝而不是报用户名密码错误:其次远程桌面服务器是可以访问我个人点的D.E盘的 打开默认共享方法:先在控制面板的“服务”,看SERVER服务是否启动,如果没有启 ...
- Appium+python自动化-Remote远程控制
前言 在第三篇启动app的时候有这样一行代码driver = webdriver.Remote('http://192.168.1.1:4723/wd/hub', desired_caps),很多小伙 ...
- Html5新瓶装老酒之一--Touch事件处理
移动端的应用越来越多的开始采用html5来实现的.Html5有许多新特性需要开发者注意,比如css3,touch事件等等.比如做一个轮播图,分析其实现要领,有三点: 1.图片的轮播效果对应的css 样 ...
- C++ std::vector
std::vector template < class T, class Alloc = allocator<T> > class vector; // generic te ...
- CentOS JAVA安装及查看路径方法
一.安装: 方法一:手动解压JDK的压缩包,然后设置环境变量 1.在/usr/目录下创建java目录 [root@localhost ~]# mkdir/usr/java[root@localhost ...
- JVM类加载机制详解
引言 如下图所示,JVM类加载机制分为五个部分:加载,验证,准备,解析,初始化,下面我们就分别来看一下这五个过程. 加载 在加载阶段,虚拟机需要完成以下三件事情: 1)通过一个类的全限定名来获取定义此 ...
- [SoapUI] 通过SoapUI发送POST请求,请求的body是JSON格式的数据
通过SoapUI发送POST请求,请求的body是JSON格式的数据: data={"currentDate":"2015-06-19","reset ...