1、题目:

给定一个字符串s,判断其是否回文词

2、思路:

采用双端队列实现,从两边弹出字母,判断是否相同,若相同则继续,直到队列中只剩一个或者没有元素。

3、代码:

from collections import deque
def palchecker(s):
    q=deque(s)
    i=0
    while i<len(s)//2:
        if len(q)==0 or len(q)==1:
            return True
        if q.pop()==q.popleft():
            continue
        else:
            return False
        i+=1
        
s='abbcca'
print(palchecker(s))

 

相关文章:

  • 2022-03-01
  • 2022-02-26
  • 2022-12-23
  • 2021-06-22
  • 2022-12-23
  • 2022-12-23
  • 2021-04-12
猜你喜欢
  • 2021-07-06
  • 2021-09-03
  • 2022-12-23
  • 2021-07-14
  • 2022-12-23
  • 2021-05-27
相关资源
相似解决方案