LeetCode 821 Shortest Distance to a Character 解题报告
题目要求
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:
Sstring length is in[1, 10000].Cis a single character, and guaranteed to be in stringS.- All letters in
SandCare 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 解题报告的更多相关文章
- 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...
- 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&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] 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 ...
- 【LeetCode】1182. Shortest Distance to Target Color 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+二分查找 日期 题目地址:https://lee ...
- 821. Shortest Distance to a Character
class Solution { public: vector<int> shortestToChar(string S, char C) { int len=S.length(); ve ...
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Emacs常用基本操作
按键约定 组合按键 Emacs中大量的按键操作都是各式各样的组合按键(快捷键),下面是几种通常的约定: Ctrl键:表示为C Alt键:表示为M Shift键:表示为S 组合按键:比如向下移动一行的组 ...
- 聊聊Python中的生成器和迭代器
Python中有两个重要的概念,生成器和迭代器,这里详细记录一下. 1. 生成器 什么是生成器呢? 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包 ...
- azkaban在centos下的部署安装
azkaban 是一个用Java开发的开源调度工具workflow. 下面介绍具体安装过程,我这里使用的版本是3.43.0,使用的是solo运行模式. 编译 git clone https://git ...
- .NET Memory Allocation Profiling with Visual Studio 2012
.NET Memory Allocation Profiling with Visual Studio 2012 This post was written by Stephen Toub, a fr ...
- Fedora Server 21下OpenJdk和Oracle Jdk共存
最新文章:Virson's Blog 参考文章:博客园-三维蚂蚁 Linux公社 1.首先需要下载对应平台的Jdk:Oracle 官网 2.使用yum或rpm命令安装Jdk: yum install ...
- ganglia分布式监控部署
一.介绍 Ganglia是由UC Berkeley发起的一个开源监控项目,设计用于监控数以千几的节点.每台服务器都运行一个收集和发送监控数据名为gmond的守护进程.它将从操作系统和指定 ...
- C++内存管理(转)
C++内存管理 [导语] 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟的收获则是一遍一遍的检查代码和对C++的痛恨,但内存管理 ...
- Linux permission denied解决方法
一.准备 比如建立一个test.sh的脚本文件,脚本代码如下: #! /bin/bash echo Hello,world! 代码功能实现的是:输出字符Hello,world! 二.运行脚本 在终端切 ...
- 6.24 html书城Demo
<!DOCTYPE html> <!-- 睡觉,2018-6-24 22:04:36 还有foot没弄好明天弄--> <html> <head> < ...
- 02Modify.ashx(修改班级信息)
02Modify.html 修改 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> & ...