[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 ...
随机推荐
- GDI/GDI+这些破事
本文是杂篇,纯属笔记,想到哪写到那! 1.获取像素的RGB以及填充 CPaintDC dc(m_hWnd); COLORREF color=dc.GetPixel(,); int R=GetRValu ...
- Oracle 创建表空间借鉴 保留,占版权留言告知
/*分为四步 */ /*第1步:创建临时表空间 */ create temporarytablespace user_temp tempfile 'D:\oracle\oradata\Oracle9i ...
- Swift 延迟运行代码
// // DelayRun.swift // // Created by XuQing on 16/7/1. // Copyright © 2016年 xuqing. All rights rese ...
- win10 IIS 10.0 无法安装 URL Rewrite Module 重写模块
打开注册表 win+R 输入 regidit在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp位置 修改注册表 把MajorVersion的值改为9 安装 ...
- 用django框架开发一个B2C购物网站用户注册知识点总结2
一:用户部分: 用户注册: 用户注册序列化器: import re from django_redis import get_redis_connection from rest_framework ...
- mysql存储过程(procedure)
#创建带参数的存储过程 delimiter // ),out p int) begin ; end // delimiter call pro_stu_name_pass(@n,@p); select ...
- Perl 引用:引用就是指针,Perl 引用是一个标量类型可以指向变量、数组、哈希表(也叫关联数组)甚至子程序。
Perl 引用引用就是指针,Perl 引用是一个标量类型可以指向变量.数组.哈希表(也叫关联数组)甚至子程序,可以应用在程序的任何地方. 1.创建引用1.使用斜线\定义变量的时候,在变量名前面加个\, ...
- Go语言的基本类型转换-golang类型转换
简单整理一下Golang的基本类型转换,作为备忘: 1.整形到字符串: var i int = 1 var s string 3. s = strconv.Itoa(i) 或者 s = Format ...
- SpringBoot30 整合Mybatis-Plus、整合Redis、利用Ehcache实现二级缓存、利用SpringCache和Redis作为缓存
1 环境说明 JDK: 1.8 MAVEN: 3. SpringBoot: 2.0.4 2 SpringBoot集成Mybatis-Plus 2.1 创建SpringBoot 利用IDEA创建Spri ...
- 手动编译cloudfoundry
1.下载cloudfoundry源代码 git clone 2.BOSH的官方定义 BOSH是一个针对大规模分布式系统的部署和生命周期管理的开源工具,其基础是"a tool of relea ...