当前位置:网站首页>力扣——求一组字符中的第一个回文字符
力扣——求一组字符中的第一个回文字符
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循环时,要对循环的运行和停止有一定的了解。本题可用函数,可不用,但力扣对格式有着严格的要求,必须要使用函数才可以
边栏推荐
- [programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array
- C语言实现【三子棋游戏】(步骤分析和实现源码)
- 【编程强训2】排序子序列+倒置字符串
- 2022 operation of refrigeration and air conditioning equipment operation of national question bank simulated examination platform
- Todolist classic case ①
- 未来互联网人才还稀缺吗?哪些技术方向热门?
- Caesar
- [recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario
- C语言实现【扫雷游戏】完整版(实现源码)
- C language implementation [minesweeping game] full version (implementation source code)
猜你喜欢

Mysql与Redis一致性解决方案

【剑指offer&牛客101】中那些高频笔试,面试题——链表篇

Paging in servlets and JSPS

How to create an exclusive vs Code theme

Redisson uses the full solution - redisson official document + comments (Part 2)

Eigen matrix operation Library

H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?

Custom events of components ②

LeetCode+ 71 - 75

ctfshow-web352,353(SSRF)
随机推荐
【编程强训2】排序子序列+倒置字符串
Redisson uses the full solution - redisson official documents + comments (Part 2)
C# Newtonsoft.Json中JObject的使用
奥迪AUDI EDI 项目中供应商需要了解哪些信息?
Is the account opening of GF Securities safe and reliable? How to open GF Securities Account
C语言实现【扫雷游戏】完整版(实现源码)
Detailed explanation of weback5 basic configuration
2022年茶艺师(中级)复训题库及答案
The computer has a network, but all browser pages can't be opened. What's the matter?
TCP/UDP 通信问题整理
2022危险化学品经营单位主要负责人试题及模拟考试
Cadence OrCAD capture "network name" is the same, but it is not connected or connected incorrectly. The usage of nodeName of liberation scheme
1286_ Implementation analysis of task priority setting in FreeRTOS
C language implementation [Sanzi chess game] (step analysis and implementation source code)
redisson看门狗机制,redisson看门狗性能问题,redisson源码解析
2022 electrician (intermediate) recurrent training question bank and answers
【深圳IO】精确食品称(汇编语言的一些理解)
Is it safe and reliable for Huatai Securities to open an account? How to open Huatai Securities Account
The database is locked. Is there a solution
[image processing] image histogram equalization system with GUI interface