题目要求

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:

  1. S string length is in [1, 10000].
  2. is a single character, and guaranteed to be in string S.
  3. All letters in and C are 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 解题报告的更多相关文章

  1. 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...

  2. 821. Shortest Distance to a Character - LeetCode

    Question 821. Shortest Distance to a Character Solution 思路:遍历字符串S,遇到与字符C相等就分别向左/右计算其他字符与该字符的距离,如果其他字 ...

  3. 【Leetcode_easy】821. Shortest Distance to a Character

    problem 821. Shortest Distance to a Character solution1: class Solution { public: vector<int> ...

  4. [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 ...

  5. [Solution] 821. Shortest Distance to a Character

    Difficulty: Easy Problem Given a string S and a character C, return an array of integers representin ...

  6. [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 ...

  7. 【LeetCode】1182. Shortest Distance to Target Color 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+二分查找 日期 题目地址:https://lee ...

  8. 821. Shortest Distance to a Character

    class Solution { public: vector<int> shortestToChar(string S, char C) { int len=S.length(); ve ...

  9. 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. moment.js返回本周

    项目中需要做个打卡的模块.里面有个模块需要返回当前这个星期从星期日到星期六的日期,如下图: 我是通过 moment.js 的 moment().day() 实现这个效果的,它的说明如下图: 关于这个插 ...

  2. Java知多少(76)语言包(java.lang)简介

    Java语言包(java.lang)定义了Java中的大多数基本类,由Java语言自动调用,不需要显示声明.该包中包含了Object类,Object类是整个类层次结构的根结点,同时还定义了基本数据类型 ...

  3. msm codec 代码跟踪

    sound/soc/codecs/msm8x16-wcd.c static struct spmi_device_id msm8x16_wcd_spmi_id_table[] = { {"w ...

  4. vector、map 内存释放

    一.vector void TestVector() { cout << "begin create vector" << endl; int iSize ...

  5. 向json中添加新的熟悉或对象 Add new attribute (element) to JSON object using JavaScript

    How do I add new attribute (element) to JSON object using JavaScript? JSON stands for JavaScript Obj ...

  6. nginx-启动gzip、虚拟主机、请求转发、负载均衡

    一.启用gzip 1     gzip  on; 2     gzip_min_length 1k; 3     gzip_buffers 4 16k; 4     gzip_http_version ...

  7. 8 -- 深入使用Spring -- 5...1 启用Spring缓存

    8.5.1 启用Spring缓存 Spring配置文件专门为缓存提供了一个cache:命名空间,为了启用Spring缓存,需要在配置文件中导入cache:命名空间. 导入cache:命名空间之后,启用 ...

  8. mongodb new file allocation failure

    话说那天正在向mongodb中写入数据,突然就蹦出了 new file allocation failure ,以为是数据有错误,就检查了一番,可没问题啊,看着像是mongo自己的问题,于是百度了一番 ...

  9. 可访问性(Accessibility) => 无障碍功能

    了解无障碍功能及其范围和影响可令您成为更出色的网络开发者 复杂的一笔 https://developers.google.cn/web/fundamentals/accessibility/ ARIA ...

  10. MyISAM和InnoDB区别 及选择

    MySQL默认采用的是MyISAM. MyISAM不支持事务,而InnoDB支持.InnoDB的AUTOCOMMIT默认是打开的,即每条SQL语句会默认被封装成一个事务,自动提交,这样会影响速度,所以 ...