[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 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:
Sstring length is in[1, 10000].Cis a single character, and guaranteed to be in stringS.- All letters in
SandCare 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的更多相关文章
- LeetCode 821 Shortest Distance to a Character 解题报告
题目要求 Given a string S and a character C, return an array of integers representing the shortest dista ...
- [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 ...
- [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 ...
- 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】821. Shortest Distance to a Character 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...
- [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] 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 ...
随机推荐
- 【大数据系列】在windows下连接linux 下的hadoop环境进行开发
一.下载Eclipse并安装 二.下载exlipse的hadoop plugin 三.打开Map Reduce视图 Window --> Perspective --> Open pers ...
- initializer element is not constant 问题
在Ubuntu下,比葫芦画瓢,写了一个程序,居然报错!!!! #include <stdio.h> ; int j = *(int *)(&i) ; int main (int a ...
- win7 查看当前java路径
C:\Users\zh>where javaC:\Windows\System32\java.exeD:\TOOL\jdk1.8.0_91\bin\java.exeD:\TOOL\jdk1.8. ...
- Adobe Acrobat Reader DC For Android 下载
http://get.adobe.com/cn/reader/otherversions/ 点击“立即下载”按钮,即可开始下载到PC端
- 升级后重启造成fsck.ext3: Unable to resolve UUID
这篇文章帮了我的大忙了:转载自:http://wilber82.blog.51cto.com/1124820/724472 今天在做服务器补丁部署,有一台ESX4.1的服务器在升级后重启过程中挂了,通 ...
- tornado web开发
tornado是python的web框架,这里简单记录下利用tornado怎么实现文件的上传,其中web.py上传功能类似. 直接用代码说明: 代码来自:http://my.oschina.net ...
- Asp.Net MVC WebApi2 自动生成帮助文档
WebAPI Help文档配置 开发环境VS2013+mvc5+WebApi2 一.通过NuGet引用Web API Test Client 安装后会多一个Areas文件夹 二.设置xml文档项目-- ...
- 在 NHibernate 中一切必须是 Virtual 的吗?
原文地址:Must Everything Be Virtual With NHibernate? 老赵在博文中 我对NHibernate的感受(2):何必到处都virtual 提到这篇文章,顺便翻译一 ...
- yii---往对象里面添加属性
我们在用YII的时候,查询到一条数据,但是很多时候会往这条查询的数据里,添加某个字段,但是直接添加会报错: $thread = $this->getThreadService()->get ...
- 算法学习之快速排序的C语言实现
近几天在学习简单算法,今天看了一个快速排序和堆排序,堆排序还没搞懂,还是先把快速排序搞清楚吧 教程网上一艘一大堆,这里选择一个讲的比较通俗的的一个吧: http://blog.csdn.net/mor ...