[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 ...
随机推荐
- 九度OJ小结2
由于安排问题,距离上次小结时间已经过去很久.导致这次小结的内容很多. 本次小结涉及到主要内容如下所示: 基于并查集操作的最小生成树问题(prime算法或者kruskal算法): 最短路径问题(Floy ...
- github命令行下载项目源码
一.git clone [URL] 下载指定ur的源码 $ git clone https://github.com/jquery/jquery 二.指定参数, -b是分支, --depth 1 最新 ...
- Windows Server 2008 R2之五操作主控的管理
一.概述 操作主控(FSMO)也称作操作主机(OM),它是指在AD中一个或多个特殊的DC,用来执行某些特殊的功能(资源标识符SID分配.架构修改.PDC选择等). 1.操作主控的分类 基于森林的操作主 ...
- wpgcms---单片页数据渲染
单片页数据渲染,使用Twig的标签语法: <h1> {{ contentInfo.title }} </h1> {% autoescape false %} {{ conten ...
- 单引号、双引号、int和char
首先说一下C语言中用单引号和双引号的不同(一直搞不清楚): 单引号代表的是一个整数,而这个整数的值是编译器所采用的字符集中的字符序列对应的值.所以一般'A'和ASCII中的65意义相同.对于双引号定义 ...
- python的for else组合用法
如下代码,输入评论,如果评论中含有敏感词则更换成*号,否则正常输入. li = ["老师", "你好", "333", "4444 ...
- 对crf++的template的理解 ©seven_clear
这是以前的一篇草稿,当初没写完,今天发出来,但总觉得水平有限,越学越觉得自己菜,写的博客水准低,发完这篇以后就谨慎发博了,毕竟自己菜,不能老吹B,下面是原稿. 好久没更了,本来年前想写篇关于爬虫的总结 ...
- NGINX域名跳转案列
1.不同域名不同路径跳转 nginx实现a.com/teacher域名跳转到b.com/student 若想实现上面题目的跳转,目前鄙人知道两种方式: 1.return 2.proxy_pass 具体 ...
- POJ-1414 Life Line (暴力搜索)
Life Line Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 855 Accepted: 623 Description L ...
- HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...