回文(palindrome):指的是从头读到尾与从尾读到头一模一样的字符串。

分别在C、Java与Python实现回文检测:

C:

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h> #define MAX_LEN 255 int main(int argc, char *args[]){
char message[MAX_LEN];
char str[MAX_LEN];
char ch;
int index = ; printf("Please enter a message: ");
while((ch = getchar()) != '\n'){
if(index==MAX_LEN){
while(getchar() != '\n'){
continue;
}
break;
}else{
message[index++] = ch;
}
} int j = ;
for(int i = ; i < index; i++){
ch = message[i];
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
str[j++] = tolower(ch);
}
} for(int i = ; i < j / ; i++){
if(str[i] != str[j-i-]){
puts("Not a Palindrome!");
return ;
}
}
puts("Palindrome!"); return ;
}

Java:

import java.util.Scanner;

public class Palindrome{
public static boolean isPalindrome(String raw){
String str = "";
// 只拿raw字符串里的字母,拼接到str里
for(int i = 0; i < raw.length(); i++){
char ch = raw.charAt(i);
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
str += ch;
}
}
// str字母全部小写化
str = str.toLowerCase();
// 判断是否为回文
int end = str.length();
for(int i = 0; i < end/2; i++){
if(str.charAt(i) != str.charAt(end-i-1)){
return false;
}
} return true;
} public static void main(String[] args){
Scanner scanner = new Scanner(System.in); // I prefer pi!
// A man, a plan, a canal: Panama!
// Madam, I am Adam.
System.out.printf("Enter a message: ");
String str = scanner.nextLine(); if(isPalindrome(str)){
System.out.println("Palindrome!");
}else{
System.out.println("Not a palindrome!");
}
}
}

Python:

import string

def is_palindrome(text: str) -> bool:
'是否为回文'
# 1、先去除标点符号以及空格,并将所有字母小写化
result = ''
for i in range(len(text)):
if not text[i] in string.punctuation + ' ':
result += text[i].lower()
print(result) # 2、判断是否为回文
n = len(result)
for i in range(len(result) // 2):
if result[i] != result[n-i-1]:
return False
return True if __name__ == '__main__':
print(is_palindrome('I prefer pi.'))
print(is_palindrome('A man, a plan, a canal: Panama.'))
print(is_palindrome('Resistance is futile!'))

算法——回文(palindrome)的更多相关文章

  1. 【Python】回文palindrome——利用字符串反转

    回文 palindrome Python 字符串反转string[::-1] Slice notation "[a : b : c]" means "count in i ...

  2. manacher算法——回文串计算的高效算法

    manacher算法的由来不再赘述,自行百度QWQ... 进入正题,manacher算法是一个高效的计算回文串的算法,回文串如果不知道可以给出一个例子:" noon ",这样应该就 ...

  3. 【面试笔试算法】Problem 9: 腾讯2016年研发实习笔试题:最长回文子串

    (一)题目 问题:求给定字符串s的回文(palindrome)子串中,长度最大的回文子串的长度. 回文(palindrome)是指从左往右读和从右往左读字符串,看到的字符串都是一样的.比如" ...

  4. 5. Longest Palindromic Substring(最长回文子串 manacher 算法/ DP动态规划)

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  5. 【翻译】Longest Palindromic Substring 最长回文子串

    原文地址: http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html 转载请注明出处:http:// ...

  6. 转:C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文

    转自:C语言字符串操作函数 - strcpy.strcmp.strcat.反转.回文 C++常用库函数atoi,itoa,strcpy,strcmp的实现 作者:jcsu C语言字符串操作函数 1. ...

  7. C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文

    原文:http://www.cnblogs.com/JCSU/articles/1305401.html C语言字符串操作函数 1. 字符串反转 - strRev2. 字符串复制 - strcpy3. ...

  8. Palindrome(最长回文串manacher算法)O(n)

     Palindrome Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  9. 【算法】最长回文子串 longest palindrome substring

    对于字符串S, 要找到它最长的回文子串,能想到的最暴力方法,应该是对于每个元素i-th都向左向右对称搜索,最后用一个数组span 记录下相对应元素i-th为中心的回文子串长度. 那么问题来了: 1. ...

随机推荐

  1. UVA 10852 Less Prime 题解

    Less Prime Let n be an integer, 100 n 10000, nd the prime number x, x n, so that n

  2. springboot 获取到的inputStream为空的问题

    springboot在接收http请求的时候读取的request的inputStream,造成我们想自己读取inputStream的时候发现inputStream已经无法读取了. 为了读取inputS ...

  3. 部门工资前三高的所有员工 - LeetCode

    Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId . +----+-------+--------+---- ...

  4. Linux 安装Redis4.0.8【yum安装】

    .下载yum源 yum install epel-release2.安装redisyum install redis3.启动redis # 启动redis service redis start # ...

  5. openresty 报错:lua entry thread aborted: runtime error

    [1]问题现象 (1)本地openresty系统 (2)报错信息 2019/09/10 08:13:55 [error] 2385#2385: *4 lua entry thread aborted: ...

  6. ASP.NET MVC+Easyui 后台管理系统的图片上传

    实现图片的上传 easyui代码部分: //添加按钮 var URL; $("#btnCreate").click(function () { $('#UserDialog').d ...

  7. 关于插件Markdown Preview Enhanced的使用技巧

    目录 1.关于TOC 2.关于转义符 3.绘图 3.0 绘图配色主题 3.1 Flowchart(流程图) 3.2 Sequence diagram(顺序图) 3.4 保存为HTML shanzm 1 ...

  8. 第一个APP上架IOS审核相关的记录

    以前一直没做过APP开发,第一版是用WAP版做的,采用了light7框架制作,没有UI设计. 升级到第二版之后,使用了HBUILDER的方式开发,https://dcloud.io/ 官方在这里. 目 ...

  9. python plotly 画饼状图

    代码 import pandas as pd import numpy as np import plotly.plotly as py import plotly.graph_objs as go ...

  10. 离散数学交并补运算、差运算、异或运算的实现--biaobiao88

    对集合的交并补运算.差运算及异或运算的代码,可输入字符与数字,内容简单,详情请看以下代码 #include<iostream> using namespace std; int main( ...