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

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

C:

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <ctype.h>
  4.  
  5. #define MAX_LEN 255
  6.  
  7. int main(int argc, char *args[]){
  8. char message[MAX_LEN];
  9. char str[MAX_LEN];
  10. char ch;
  11. int index = ;
  12.  
  13. printf("Please enter a message: ");
  14. while((ch = getchar()) != '\n'){
  15. if(index==MAX_LEN){
  16. while(getchar() != '\n'){
  17. continue;
  18. }
  19. break;
  20. }else{
  21. message[index++] = ch;
  22. }
  23. }
  24.  
  25. int j = ;
  26. for(int i = ; i < index; i++){
  27. ch = message[i];
  28. if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
  29. str[j++] = tolower(ch);
  30. }
  31. }
  32.  
  33. for(int i = ; i < j / ; i++){
  34. if(str[i] != str[j-i-]){
  35. puts("Not a Palindrome!");
  36. return ;
  37. }
  38. }
  39. puts("Palindrome!");
  40.  
  41. return ;
  42. }

Java:

  1. import java.util.Scanner;
  2.  
  3. public class Palindrome{
  4. public static boolean isPalindrome(String raw){
  5. String str = "";
  6. // 只拿raw字符串里的字母,拼接到str里
  7. for(int i = 0; i < raw.length(); i++){
  8. char ch = raw.charAt(i);
  9. if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
  10. str += ch;
  11. }
  12. }
  13. // str字母全部小写化
  14. str = str.toLowerCase();
  15. // 判断是否为回文
  16. int end = str.length();
  17. for(int i = 0; i < end/2; i++){
  18. if(str.charAt(i) != str.charAt(end-i-1)){
  19. return false;
  20. }
  21. }
  22.  
  23. return true;
  24. }
  25.  
  26. public static void main(String[] args){
  27. Scanner scanner = new Scanner(System.in);
  28.  
  29. // I prefer pi!
  30. // A man, a plan, a canal: Panama!
  31. // Madam, I am Adam.
  32. System.out.printf("Enter a message: ");
  33. String str = scanner.nextLine();
  34.  
  35. if(isPalindrome(str)){
  36. System.out.println("Palindrome!");
  37. }else{
  38. System.out.println("Not a palindrome!");
  39. }
  40. }
  41. }

Python:

  1. import string
  2.  
  3. def is_palindrome(text: str) -> bool:
  4. '是否为回文'
  5. # 1、先去除标点符号以及空格,并将所有字母小写化
  6. result = ''
  7. for i in range(len(text)):
  8. if not text[i] in string.punctuation + ' ':
  9. result += text[i].lower()
  10. print(result)
  11.  
  12. # 2、判断是否为回文
  13. n = len(result)
  14. for i in range(len(result) // 2):
  15. if result[i] != result[n-i-1]:
  16. return False
  17. return True
  18.  
  19. if __name__ == '__main__':
  20. print(is_palindrome('I prefer pi.'))
  21. print(is_palindrome('A man, a plan, a canal: Panama.'))
  22. 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. hdfs 列出文件

    package com.lala.lala.pipe.dbinfo import java.io.{ByteArrayOutputStream, PrintWriter} import com.ali ...

  2. 转载:string、const char*、 char* 、char[]相互转换

    本文转自:https://blog.csdn.net/rongrongyaofeiqi/article/details/52442169 一:转化总结形式如下: 使用时,要对源格式和目标格式进行初始化 ...

  3. 2.1:CGPROGRAM

    文章著作权归作者所有.转载请联系作者,并在文中注明出处,给出原文链接. 本系列原更新于作者的github博客,这里给出链接. 前言 经过前面两个章节的铺垫,我们对渲染以及Unity Shaderlab ...

  4. Java SPI机制:ServiceLoader实现原理及应用剖析

    一.背景 SPI,全称Service Provider Interfaces,服务提供接口.是Java提供的一套供第三方实现或扩展使用的技术体系.主要通过解耦服务具体实现以及服务使用,使得程序的可扩展 ...

  5. k8s网络原理

    https://blog.csdn.net/watermelonbig/article/details/80646988 k8s中,每个 Pod 都有一个独立的 IP 地址,所有 Pod 在一个网络空 ...

  6. angular 监听离开页面执行相关操作

    $scope.$on("$destroy", function() { //...})

  7. 【spring】spring retry介绍

    一.为什么需要重试? 我们知道只要是网络请求都有失败的情况,这个时候增加retry机制是必要的.而spring全家桶中就有这么一套机制. 二.spring retry spring系列的spring ...

  8. Java常用类object详解

    1.Object概述: 类Object是类层次结构的根类.每个类都使用Object作为超类.所有对象(包括数组)都实现这个类的方法. 2.构造方法详细信息: Object只有一个无参构造方法,因为ob ...

  9. 我为啥不想用Python

    Python这门语言从一开始就是一个玩具语言,它不是给正经程序员用的东西. 运行效率低 Python运行效率很低,这就导致Python中很多库底层实际上是C++.很多时候,自己千方百计优化的结果就不如 ...

  10. Java 之 JDK 1.8 新增日期时间类型

    一.原来的日期时间 Java1.0中包含了一个Date类,但是它的大多数方法已经在Java 1.1引入Calendar类之后被弃用了.而Calendar并不比Date好多少.它们面临的问题是: ① 可 ...