题目要求

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. spring aop 之annotation

    1.CutPointInterface public interface CutPointInterface { void method(); } 2.CutPointClass @Component ...

  2. 【Unity】Protobuf的使用与常见问题

    Protobuf的使用流程 protobuf参考教程:https://www.jianshu.com/p/b135676dbe8d 手写.proto文件后,用CMD命令行运行protoc.exe编译器 ...

  3. 解剖 Elasticsearch 集群 - 之一

    解剖 Elasticsearch 集群 - 之一 本篇文章是一系列涵盖 Elasticsearch 底层架构和原型示例的其中一篇.在本篇文章中,我们会讨论底层的存储模型以及 CRUD(创建.读取.更新 ...

  4. Manager升职了

    公司去年从每年七月份公布officer升职改成了每年四月份公布. 早上收到大头发给全公司的邮件,赫然发现Manager升了一级到VP,虽然是金融公司,但我司的VP好像会比银行多一点点福利,比如额外假期 ...

  5. 什么是位、字节、字、KB、MB (转)

    回顾一下按位操作符和移位操作符的知识,顺便复习一下位相关的基础知识. 位:"位(bit)"是电子计算机中最小的数据单位.每一位的状态只能是0或1. 字节:8个二进制位构成1个&qu ...

  6. java获取视频缩略图

    近期由于在做一个关于视频播放的项目,需要使用程序自动获取视频文件的缩略图,特写此文供其他人参考,有不清楚之楚可以给我留言. 1.使用工具:ffmpeg, 官网下载地址:http://ffmpeg.or ...

  7. [Object Tracking] Contour Detection through Tensorflow running on smartphone

    From: 手机端运行卷积神经网络的一次实践 -- 基于 TensorFlow 和 OpenCV 实现文档检测功能 貌似不错的东西:移动端视觉识别模型:MobileNets Holistically- ...

  8. [UFLDL] Linear Regression & Classification

    博客内容取材于:http://www.cnblogs.com/tornadomeet/archive/2012/06/24/2560261.html Deep learning:六(regulariz ...

  9. [React] 11 - Redux: redux

    Ref: Redux中文文档 Ref: React 讀書會 - B團 - Level 19 Redux 深入淺出 Ref: React+Redux 分享會 Ruan Yifeng, Redux 架构: ...

  10. 《objective-c基础教程》学习笔记(二)—— for循环的基本应用

    在上一篇博文中,我们介绍了如何开发前期的准备,以及简单的类型输出实例. 这篇博文,我要记录一个for循环输出的实例.并对此展开,改变成不同的三个小函数. int main(int argc, cons ...