`
jgsj
  • 浏览: 962026 次
文章分类
社区版块
存档分类
最新评论

Leetcode Valid Palindrome

 
阅读更多

Valid Palindrome

Total Accepted:6669Total Submissions:31102My Submissions

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"isnota 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.


很简单的题目: 2星级。

考点:

1 会判断是否是字母和数字

2 知道什么是Palindrome


可以利用函数库判断是否为数字或者字母,程序如下:

bool isPalindrome(string s) 
	{
		for (int i = 0, j = s.size()-1; i < j; i++, j--)
		{
			while (i<j && (!isalpha(s[i]) && !isdigit(s[i]))) 
				i++;
			while (i<j && (!isalpha(s[j]) && !isdigit(s[j]))) 
				j--;
			
			if (i<j && tolower(s[i]) != tolower(s[j])) return false;
		}
		return true;
	}

自家写函数判断也很简单:

bool isPalindrome(string s) 
	{
		for (int i = 0, j = s.size()-1; i < j; i++, j--)
		{
			while (s[i] && (!isTestChar(s[i]))) i++;
			while (s[j] && (!isTestChar(s[j]))) j--;
			
			if (i<j && !isEqual(s[i], s[j])) return false;
		}
		return true;
	}

	bool isTestChar(char a)
	{
		int i = a;
		return (i>=97 && i<=122) || (i>=65 && i<=90) || (i>=48 && i<=57);
	}
	bool isEqual(char a, char b)
	{
		return a == b || int(a) == int(b)-32 || int(b) == int(a)-32;
	}
	/*
	char a = 'A';//65
	a = 'a';//97
	a = 'Z';//90
	a = 'z';//122
	a = '1';//49
	a = '0';//48
	*/

//2014-2-18 update
	bool isPalindrome(string s) 
	{
		for (int i = 0, j = s.length()-1; i < j; )
		{
			if (isalnum(s[i]) && isalnum(s[j]))
			{
				if (tolower(s[i]) != tolower(s[j])) return false;
				i++, j--;
			}
			if (i<j && !isalnum(s[i])) i++;
			if (i<j && !isalnum(s[j])) j--;
		}
		return true;
	}



分享到:
评论

相关推荐

    javalruleetcode-LeetCode:LeetCode算法问题

    Palindrome LeetCode 167 Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels of a String 2 字符串 编号 题目 LeetCode 3 Longest Substring Without Repeating ...

    LeetCode最全代码

    # [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...

    erlang入门级练习:LeetCode OJ问题的部分erlang 源码

    我自己在新学erlang,在LeetCode OJ上找了题目练习,题目很适合新手熟悉语言,但是LeetCode OJ里面只有几门主流语言的... "valid_palindrome.erl" 个人认为dungeon_game这个题目解题逻辑很体现erlang的函数式的思维逻辑

    leetcode中国-leetcode:leetcode刷题

    Palindrome Implement strStr() String to Integer (atoi) addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方图的最大面积,左右两次扫面+剪枝优化 Valid Parentheses 用栈...

    leetcode不会-ValidPalindrome:这是我在Python中针对LeetCode上的ValidPalindrome问题的解决方

    Valid Palindrome 问题的解决方案。 首先,我将给定字符串中的所有字符都小写,这样就不会有任何违规行为。 然后我创建两个空数组。 在一个数组中,我将给定字符串的所有字母数字值附加到该数组中。 在第二个中,我...

    leetcode卡-LeetCode:LeetCode题解

    Palindrome :star: 有效回文,小写字母转换 0005 Longest Palindromic Substring :star: :star: :star: 最长回文子串,Manacher算法 0010 RegularExpressionMatching :star: :star: :star: 正则表达式匹配,dp 0012 ...

    leetcode516-Lcode:代码

    leetcode 516 8/13 - 8/18 周:leetcode#: ...Valid Palindrome 28. Implement strStr() 5. Longest Palindromic Substring option: 516. Longest Palindromic Subsequence string replacement strStr II 链接:

    leetcode浇花-LCSolutions:我的力扣解决方案

    leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates...

    程序员面试宝典LeetCode刷题手册

    第四章 Leetcode 题解 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most ...

    leetcodepython001-LeetCode:力码

    leetcode Python 001 力码 简单的 # 问题 完成日期 语 001 001_Two_Sum 2021 年 1 月 5 日 Python 002 007_Reverse_Integer 2021 年 1 月 7 日 Python 003 009_Palindrome_Number 2021 年 1 月 7 日 Python 004 013_...

    颜色分类leetcode-leetcode-[removed]我对Leetcode问题的解决方案

    Palindrome Number 回文数 11 Container With Most Water 盛最多水的容器 13 Roman to Integer 罗马数字转整数 14 Longest Common Prefix 最长公共前缀 20 Valid Parentheses 有效的括号 26 Remove Duplicates from ...

    leetcode分类-leetcode-practice:LeetCode题解

    leetcode 分类 ...Palindrome II Easy 数据结构 二叉树 题目 难度 原题 解答 26. 树的子结构 中等 未分类 鸣谢 题目选择、分类参考了 关于 同步自我的博客:, 欢迎关注我的微信公众号「清风迅来」

    LeetCode:LeetCode题解

    LeetCode LeetCode题解 传送门 # 标题 解决方案 困难 笔记 1个 简单的 3 ... Valid_Palindrome Java 简单的 136 单号 Java 简单的 137 Single_NumberII Java 中等的 167 Two_Sum_II_Input_

    leetcode题库-LeetCode:力码

    Palindrome Number.cpp 12 整数转罗马数字 Integer to Roman.cpp 13 罗马数字转整数 Roman to Integer.cpp 15 三数之和 3Sum.cpp 最接近的三数之和 3Sum Closest .cpp 20 有效的括号 Valid Parentheses.cpp 22 括号...

    lrucacheleetcode-leetcode:leetcode

    Palindrome Number 11. Container With Most Water 12. Integer to Roman 13. Roman to Integer 14. Longest Common Prefix 15. 3Sum 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses ...

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    gas station leetcode 在牛客网上的在线编程中的leetcode在线编程题解 代码中有详细题解 完成: 树 Minimum Depth of ...evaluate-reverse-polish-notation ...valid-palindrome 模拟 pascals-triangle 模拟 pasca

    leetcode双人赛-LeetCode:力扣笔记

    leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...

    LeetCode去除数组重复元素-leetcode_[removed]刷题

    Palindrome Number(回文数) 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。 Longest Common Prefix(最长公共前缀) 编写一个函数来查找字符串数组中的最长公共前缀,...

    leetcode卡-leetcode:利特码解决方案

    Valid Sudoku linked list Palindrome linked list Linked List Cycle trees Convert Sorted Array to Binary Search Tree string and search First Bad Version Dynamic Programing *** Climbing Stairs Set Matrix...

    leetcode530-algorithm:算法

    Palindrome Number 010 Regular Expression Matching 011 Container With Most Water 012 Integer to Roman 013 Roman to Integer 014 Longest Common Prefix 015 3Sum 016 3Sum Closest 017 Letter Combinations of...

Global site tag (gtag.js) - Google Analytics