7

检查回文数/字符串的奇数/偶数长度是个好主意吗?我遇到的大多数片段都没有做这个基本测试。如果长度是偶数,它不可能是回文,不是吗?

if len(var) % 2 != 0:
  # could be a palindrome, continue...
else:
  break

还是直接比较第一个和最后一个数字/字母更好(即更快)?

编辑:好的,愚蠢的问题,应该三思而后行!:)

4

8 回答 8

25

ABBA - 四个字母的回文示例,意思是长度均匀。

回文是一个单词、短语、数字或其他字符序列,它们向后或向前读取相同...

于 2010-01-19T17:20:30.003 回答
10

检查回文的最简单方法是简单地将字符串与其相反的字符串进行比较:

def ispalindrome(s):
   return s == s[::-1]

这使用带有负步骤的扩展切片来向后走s并得到相反的结果。

于 2010-01-19T17:36:45.050 回答
8

baab = 回文数,长度为 4,是偶数

于 2010-01-19T17:17:41.057 回答
3

试试这个:

is_palindrome = lambda s : all(s1==s2 for s1,s2 in zip(s[:len(s)/2],s[-1:-(len(s)+1)/2:-1]))

只检查前半部分和后半部分,一发现不匹配就短路。

于 2010-01-19T20:47:20.900 回答
2

简单案例:aa。

更复杂的情况:aaaa。

等等。

于 2010-01-19T17:40:19.330 回答
2

如果 string.length 是偶数 Then :所有字符数都应该是偶数,所以我们不能有一个奇数的字符。

如果 string.length 为奇数则:一个字符数必须为奇数,因此并非所有字符数都应为偶数。

--------------- 我为后续角色实现了以下 JavaScript:

function isStrPermutationOfPalindrome(_str) { // backward = forward
    var isPermutationOfPalindrome = true;
    var _strArr = [..._str];
    var _strArrLength = _strArr.length;
    var counterTable = getCharsTabularFrequencies(_str);
    var countOdd = 0;
    var countEven = 0;
    for (let [ky, val] of counterTable) {
        if (val % 2 == 0) {
            countEven = countEven + 1;
        } else {
            countOdd = countOdd + 1;
        }
    }
    if (_strArrLength % 2 == 0) {
        //Even count of all characters,otherwise false.
        //so can not have a character with odd count.
        if (countOdd != 0) {
            isPermutationOfPalindrome = false;
        }

    } else {
        //Odd count of 1 character
        //so not all chars with even count, only one char of odd count.

        if (countOdd > 1 || countOdd == 0) { //no odd, or more than one odd [ only one odd should be to return true]
            isPermutationOfPalindrome = false;
        }
    }
    return isPermutationOfPalindrome;
}


function getCharsTabularFrequencies(str) {
    str = str.toLowerCase();
    var arr = Object.assign([], str);
    var oMap = new Map();
    var _charCount = 0;
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] === ' ') {
            continue;
        }
        _charCount = 0;
        for (let j = 1; j < arr.length; j++) {
            {
                if (arr[i] === arr[j]) {
                    _charCount = _charCount + 1;
                }
            }
        }
        if (i == 0)
            _charCount = _charCount + 1;
        if (!oMap.has(arr[i]))
            oMap.set(arr[i], _charCount)
    }
    return oMap;
}

let _str = 'tactcoapapa';
console.log("Is a string of '" + _str + "' is a permutation of a palindrome ? ANSWER => " + isStrPermutationOfPalindrome(_str));
于 2018-12-24T08:38:36.090 回答
1

即使是长度字符串也可以是回文。维基百科没有说明这个限制。

于 2010-01-19T17:20:22.033 回答
0
n=raw_input("Enter a string==>")
n=int(n)

start=0
term=n

while n>0:
    result=n%10
    start=start*10+result
    n=n/10

print start

if term==start:
    print "True"
else:
    print "False"
于 2010-08-10T09:59:10.060 回答