当前位置:网站首页>力扣——求一组字符中的第一个回文字符
力扣——求一组字符中的第一个回文字符
2022-07-01 07:30:00 【算法与编程之美】
引言
本题是本周力扣竞赛的第一题,求回文数是我们在学习python时,都会经历的。比起求回文数,求回文字符会更简单一些问题
给你一个字符串数组words,找出并返回数组中的第一个回文字符串。如果不存在满足要求的字符串,返回一个空字符串""
示列
输入words = ["abc","car","ada","racecar","cool"]
输出:"ada"方法
建立一个函数f(x),利用for in 循环来遍历字符组words的每一字符,再利用切片的方法使字符倒过来,再用条件if来判断倒过来的字符与原字符是否相等,如果遇到第一个相等后,便输出字符并结束函数,如果没有相等的,就返回””实验结果与讨论
通过实验、实践等证明提出的方法是有效的,是能够解决开头提出的问题。
代码清单def f(x):
for i in x:
d = i[::-1]:
if d == i:
return i
return “”
words = ["abc","car","ada","racecar","cool"]
print(f(words))结语
做这道题,首先要知道回文字符是什么,并且应当对切片有一定的理解,才能较快的完成这道题。应当注意,使用for in循环时,要对循环的运行和停止有一定的了解。本题可用函数,可不用,但力扣对格式有着严格的要求,必须要使用函数才可以
边栏推荐
- Understanding of Turing test and Chinese Room
- JAX的深度学习和科学计算
- 手机开户选哪个证券公司比较好,哪个更安全
- 长路漫漫、技术作伴
- How to enter the Internet industry and become a product manager? How to become a product manager without project experience?
- 运维面临挑战?智能运维管理系统来帮您
- How to draw a product architecture diagram?
- 熱烈祝賀五行和合酒成功掛牌
- Redisson watchdog mechanism, redisson watchdog performance problems, redisson source code analysis
- 如何制作专属的VS Code主题
猜你喜欢

How to draw a product architecture diagram?
![[programming training] delete public characters (hash mapping) + team competition (greedy)](/img/cd/63eb9da1e8956df0763797f079b67f.png)
[programming training] delete public characters (hash mapping) + team competition (greedy)

她就是那个「别人家的HR」|ONES 人物

redisson使用全解——redisson官方文檔+注釋(上篇)

Solution to the problem that objects in unity2021 scene view cannot be directly selected

H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?
![[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario](/img/10/ed857892d2e0ea72e100a4008e6d69.png)
[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario
![[lingo] solve quadratic programming](/img/4d/3f7de69943f29a71c4039299c547f7.png)
[lingo] solve quadratic programming

Eigen矩阵运算库快速上手

Redisson utilise la solution complète - redisson Documents officiels + commentaires (Partie 1)
随机推荐
H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?
C语言实现【三子棋游戏】(步骤分析和实现源码)
[programming training] delete public characters (hash mapping) + team competition (greedy)
C language implementation [minesweeping game] full version (implementation source code)
[Shenzhen IO] precise Food Scale (some understanding of assembly language)
2022 mobile crane driver test exercises and online simulation test
Challenges faced by operation and maintenance? Intelligent operation and maintenance management system to help you
redisson使用全解——redisson官方文档+注释(中篇)
JSP - paging
How to choose a product manager course when changing to a product manager?
1286_ Implementation analysis of task priority setting in FreeRTOS
Caesar
Is it suitable for girls to study product manager? What are the advantages?
赌上了绩效,赢了公司CTO,我要搭DevOps平台!
Unity2021-Scene视图中物体无法直接选中的解决办法
C# 读写自定义的Config文件
Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
I bet on performance and won the CTO of the company. I want to build Devops platform!
Warm congratulations on the successful listing of five elements hehe liquor
[image processing] image histogram equalization system with GUI interface