LeetCode 821 Shortest Distance to a Character 解题报告
题目要求
Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string.
Note:
Sstring length is in[1, 10000].Cis a single character, and guaranteed to be in stringS.- All letters in
SandCare lowercase.
题目分析及思路
题目给出一个字符串和一个字符,要求得到字符串中每一个字符和该字符的最短距离。可以先得到已知字符的所有索引,再遍历字符串得到每一个字符与该字符的最短距离。
python代码
class Solution:
def shortestToChar(self, S: 'str', C: 'str') -> 'List[int]':
index = []
res = []
for i, v in enumerate(S):
if v == C:
index.append(i)
for i, v in enumerate(S):
res.append(min(list(map(lambda x:abs(x-i), index))))
return res
LeetCode 821 Shortest Distance to a Character 解题报告的更多相关文章
- 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...
- 821. Shortest Distance to a Character - LeetCode
Question 821. Shortest Distance to a Character Solution 思路:遍历字符串S,遇到与字符C相等就分别向左/右计算其他字符与该字符的距离,如果其他字 ...
- 【Leetcode_easy】821. Shortest Distance to a Character
problem 821. Shortest Distance to a Character solution1: class Solution { public: vector<int> ...
- [LeetCode&Python] Problem 821. Shortest Distance to a Character
Given a string S and a character C, return an array of integers representing the shortest distance f ...
- [Solution] 821. Shortest Distance to a Character
Difficulty: Easy Problem Given a string S and a character C, return an array of integers representin ...
- [LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS
Given a string S and a character C, return an array of integers representing the shortest distance f ...
- 【LeetCode】1182. Shortest Distance to Target Color 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+二分查找 日期 题目地址:https://lee ...
- 821. Shortest Distance to a Character
class Solution { public: vector<int> shortestToChar(string S, char C) { int len=S.length(); ve ...
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- C#学习笔记(33)——批量修改word标题
说明(2017-12-22 11:20:44): 1. 因为数学脚本的主标题和副标题没有格式,目录导航里不显示,修改的时候不好定位,所以需要改成下图格式: 2. 问题的难点有两个,一个是word的操作 ...
- Android Launcher分析和修改11——自定义分页指示器(paged_view_indicator)
Android4.0的Launcher自带了一个简单的分页指示器,就是Hotseat上面那个线段,这个本质上是一个ImageView利用.9.png图片做,效果实在是不太美观,用测试人员的话,太丑了. ...
- 【iCore4 双核心板_FPGA】例程一:GPIO输出实验——点亮LED
实验现象: 三色LED循环点亮. 核心源代码: module led_ctrl( input clk_25m, input rst_n, output fpga_ledr, output fpga_l ...
- 【iCore1S 双核心板_FPGA】例程五:Signal Tapll 实验——逻辑分析仪
核心代码: //--------------------Module_Signal_TapII-------------------// module Signal_TapII( input CLK_ ...
- 一些jquery特效收集
jQuery幻灯片插件带投影的图片叠加切换幻灯片轮播 特效:http://www.jsfoot.com/jquery/images/ jquery文字滚动上下间歇文字滚动 http://www.17s ...
- Git 审查更改
但查看提交详细资料后,Jerry 实现字符串的长度不能为负数,所以他决定改变my_strlen函数的返回类型. Jerry 使用git日志命令来查看日志信息. [jerry@CentOS projec ...
- 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验三:按键模块② — 点击与长点击
实验三:按键模块② - 点击与长点击 实验二我们学过按键功能模块的基础内容,其中我们知道按键功能模块有如下操作: l 电平变化检测: l 过滤抖动: l 产生有效按键. 实验三我们也会z执行同样的事情 ...
- Scriter CSS
transition: height(quart-out,1.0s,quart-in); transform:rotate(50deg); http://www.terrainformatica.co ...
- 试着理解cookie和session
web服务一个特点是提供个性化服务,为很多客户端提供服务,那么每个用户不同,服务自然不同,所以要记住用户及其状态,这也很多应用软件的通用功能. 由于http是无状态的,Session和Cookie是两 ...
- java连接数据库的基本操作
ResultSet保存查询的table数据,就像mysql的客户端显示的查询类容,但这中间还是藏有很多信息.