博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode刷题:125. Valid Palindrome
阅读量:4039 次
发布时间:2019-05-24

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

LeetCode刷题:125. Valid Palindrome

原题链接:

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

Example 1:

Input: "A man, a plan, a canal: Panama"Output: true

Example 2:

Input: "race a car"Output: false

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

说明:本题中,我们将空字符串定义为有效的回文串。

示例 1:

输入: "A man, a plan, a canal: Panama"输出: true

示例 2:

输入: "race a car"输出: false

算法设计

package com.bean.algorithm.basic;public class ValidPalindrome {	public static boolean isPalindrome(String input) {		input = input.toLowerCase();		input = input.replaceAll("[^0-9a-zA-Z]", "");		int j = input.length();		for (int i = 0; i < input.length(); i++) {			String fword = input.substring(i, i + 1);			String lword = input.substring(j - 1, j);			if (!fword.equals(lword)) {				return false;			}			if (i == j) {				return true;			}			j--;		}		return true;	}	public static void main(String[] args) {		// TODO Auto-generated method stub		/*		 * Input: "A man, a plan, a canal: Panama"		 * Output: true		 */		ValidPalindrome validPalindrome=new ValidPalindrome();		String input="A man, a plan, a canal: Panama";		boolean flag=validPalindrome.isPalindrome(input);		System.out.println("Flag is: "+flag);			}}

程序运行结果:

Flag is: true

转载地址:http://ajtdi.baihongyu.com/

你可能感兴趣的文章
gstreamer相关工具集合
查看>>
arm 自动升级脚本
查看>>
RS232 四入四出模块控制代码
查看>>
gstreamer插件之 videotestsrc
查看>>
autoupdate script
查看>>
linux 驱动开发 头文件
查看>>
/etc/resolv.conf
查看>>
container_of()传入结构体中的成员,返回该结构体的首地址
查看>>
linux sfdisk partition
查看>>
ipconfig,ifconfig,iwconfig
查看>>
opensuse12.2 PL2303 minicom
查看>>
网络视频服务器移植
查看>>
Encoding Schemes
查看>>
移植QT
查看>>
如此调用
查看>>
计算机的发展史
查看>>
带WiringPi库的交叉编译如何处理一
查看>>
带WiringPi库的交叉笔译如何处理二之软链接概念
查看>>
Spring事务的七种传播行为
查看>>
ES写入找不到主节点问题排查
查看>>