Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string.

Example 1:

Input: S = "loveleetcode", C = 'e'
Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]

Note:

  1. S string length is in [1, 10000].
  2. C is a single character, and guaranteed to be in string S.
  3. All letters in S and C are lowercase.

思路为BFS, 类似于[LeetCode] 286. Walls and Gates_Medium tag: BFS.

Code

class Solution(object):
def shortestToChar(self, S, C):
"""
:type S: str
:type C: str
:rtype: List[int]
"""
queue, visited, ans, l = collections.deque(), set(), [0]*len(S), len(S)
for index, ch in enumerate(S):
if ch == C:
queue.append((index,0))
visited.add(index)
while queue:
node, dis = queue.popleft()
for d in [-1,1]:
n_index = node + d
if 0 <= n_index < l and n_index not in visited:
ans[n_index] = dis + 1
visited.add(n_index)
queue.append((n_index, dis + 1))
return ans

[LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS的更多相关文章

  1. LeetCode 821 Shortest Distance to a Character 解题报告

    题目要求 Given a string S and a character C, return an array of integers representing the shortest dista ...

  2. [LeetCode] 613. Shortest Distance in a Line_Easy tag: SQL

    Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Writ ...

  3. [LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  4. 821. Shortest Distance to a Character - LeetCode

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

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

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

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

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

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

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

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

  9. [LeetCode] 317. Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

随机推荐

  1. 【SPMF开源数据挖掘平台入门】MaxSP算法使用说明

    前段时间,由于项目中用到了序列挖掘的算法,师兄推荐我用用SPMF.在此做个记录. 首先简单介绍一下SPMF: SPMF是一个采用Java开发的开源数据挖掘平台. 它提供了51种数据挖掘算法实现,用于: ...

  2. spring基础---->spring自定义初始化(一)

    这里我们简单的实现一下spring中的初始化bean,以大概了解他的流程.受委屈几乎是一个人成长最快的途径,吃下去的是委屈,消化掉后得到的是格局. spring的自定义初始化 测试的项目结构如下: 一 ...

  3. RAC迁移至单机考虑几大因素

    数据库迁移几大因素 1. 停机时间 2. 源端,目标端 操作系统平台,版本,对应的数据库版本 3. 数据量 4. 外界因素,存储空间,网络等

  4. LeetCode 46 Permutations(全排列问题)

    题目链接:https://leetcode.com/problems/permutations/?tab=Description   Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...

  5. 用CornerStone配置SVN,HTTP及svn简单使用说明

    转载 http://my.oschina.net/joanfen/blog/194491 一.下载地址 CornerStoneV2.6:http://pan.baidu.com/s/1qWEsEbM密 ...

  6. SOS does not support the current target architecture解决方法

    客户提交一个dump文件,WinDbg加载时出现大量WARNING,加载对应版本的SOS后执行相应命令提示"SOS does not support the current target a ...

  7. Maven属性(properties)标签的使用

    在命令行使用属性时,是-D,比如:mvn -D input=test Properties 属性是了解POM基础知识的最后一个要素.Maven属性是值占位符,如Ant中的属性.它们的值可以通过使用符号 ...

  8. Chrome V8引擎的一点认识

    最近在玩弄JavaScript ,也一直在捉摸,脚本Engine怎么解析你写的Code,对Google兴趣浓,索性就看了谷歌的脚本engine的官方资料,都是E文的,但是却是最纯的不是,看下来总结V8 ...

  9. 基于Token的多平台身份认证价格设计

    1   概述 在存在账号体系的信息系统中,对身份的鉴定是非常重要的事情. 随着移动互联网时代到来,客户端的类型越来越多, 逐渐出现了 一个服务器,N个客户端的格局 . 不同的客户端产生了不同的用户使用 ...

  10. 360浏览器设置打开默认为chrome极速模式

    <meta name="renderer" content="webkit"> 若页面需默认用ie兼容内核,增加标签: <meta name= ...