题解 | #判断回文#

判断回文

http://www.nowcoder.com/practice/e297fdd8e9f543059b0b5f05f3a7f3b2

1.Python 中间字符串反转解法

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
# 
# @param str string字符串 待判断的字符串
# @return bool布尔型
#
class Solution:
    def judge(self , str ):
        # write code here
        if len(str)==1:
            return True
        if len(str)%2==0:
            mid=len(str)//2
            if str[0:mid]==str[mid:][::-1]:
                return True
        else:
            mid=(len(str)-1)//2
            if str[0:mid]==str[mid+1:][::-1]:
                return True
        return False

2.Python 整体字符串反转解法

class Solution:
    def judge(self , str ):
        # write code here

        if not str:
            return True

        # 库函数
        new_str = str[::-1]

        return new_str == str
全部评论

相关推荐

评论
3
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务