[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:
S
string length is in[1, 10000].
C
is a single character, and guaranteed to be in stringS
.- All letters in
S
andC
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的更多相关文章
- 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 ...
随机推荐
- 部署OpenStack问题汇总(三)--Failed to add image
使用glance add 上传完img文件的时候出现了下面的错误 ------------------------------------------------------------------- ...
- maven打war包的过程中,都用了哪些插件呢?
一.maven生命周期 http://ifeve.com/introduction-to-the-lifecycle/ https://maven.apache.org/guides/introduc ...
- linux route命令详解
考试题一:linux下如何添加路由(百度面试题) 以上是原题,老男孩老师翻译成如下3道题. a.如何用命令行方式给linux机器添加一个默认网关,假设网关地址为10.0.0.254? b. 192.1 ...
- 【CF653G】Move by Prime 组合数
[CF653G]Move by Prime 题意:给你一个长度为n的数列$a_i$,你可以进行任意次操作:将其中一个数乘上或者除以一个质数.使得最终所有数相同,并使得操作数尽可能小.现在我们想要知道$ ...
- 【BZOJ1478】Sgu282 Isomorphism Pólya定理神题
[BZOJ1478]Sgu282 Isomorphism 题意:用$m$种颜色去染一张$n$个点的完全图,如果一个图可以通过节点重新标号变成另外一个图,则称这两个图是相同的.问不同的染色方案数.答案对 ...
- node path的一些理解笔录
例子假如我们有这样的文件结构: app/ -lib/ -common.js -model -task.js -test.js 执行代码: var path = require("path&q ...
- ubuntu下中文乱码解决方案(全)
转自 http://www.cnblogs.com/end/archive/2011/04/19/2021507.html 1.ibus输入法 Ubuntu 系统安装后已经自带了ibus输入法,在 ...
- Python内置函数之isinstance,issubclass
isinstance判断一个变量的类型 >>> n1 = 10>>> isinstance (n1,int)True 判断n1是否是数字类型,如果是返回True如果 ...
- HOJ 2985 Wavio Sequence(最长递增子序列以及其O(n*logn)算法)
Wavio Sequence My Tags (Edit) Source : UVA Time limit : 1 sec Memory limit : 32 M Submitted : 296, A ...
- 【转】JavaScript prototype
原文地址:http://www.cnblogs.com/dolphinX/p/3286177.html 用过JavaScript的同学们肯定都对prototype如雷贯耳,但是这究竟是个什么东西却让初 ...