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 ...
随机推荐
- 100种不同图片切换效果插件pageSwitch
分享100种不同图片切换效果插件pageSwitch.这是一款适用于全屏切换场景,即一切一屏,并且实现了超过一百种切换效果,支持自定义切页动画.效果图如下: 在线预览 源码下载 实现的代码. ht ...
- redis竞汰数据同步问题解决
Redis 面试的时候遇到过问Redis是如何解决“竞态条件”的,相关知识点总结一下. 乐观锁 所谓竞态条件,举个例子,一个代表点击数的数值hitcount,每个客户点击一次则+1. 没有事务的时候, ...
- Weka算法Clusterers-Xmeans源代码分析(一)
<p></p><p><span style="font-size:18px">上几篇博客都是分析的分类器算法(有监督学习),这次就分 ...
- Git服务器环境搭建(打造属于自己的存储库)
Git服务器搭建之CentOs7-阿里云搭建 首先使用连接工具连接到远程的阿里云服务器CentOs7上. 1.安装Git及创建用户 # 安装Git $ yum install git # 创建一个gi ...
- [Laravel] 01 - Love beautiful code? We do too.
前言 一.良心资料 英文 Laravel 框架:https://laravel.com/ 教程:https://laracasts.com/series/ laravel-from-scratch-2 ...
- php文件缓存方法总结
为大家分享很全的php文件缓存,供大家参考,具体内容如下 <?php class cache { private static $_instance = null; protected ...
- trace-cmd使用方法
使用trace-cmd有的时候没有来得及使用ctrl+c, 导致出现多个trace.dat.cpu*, 可以使用下面的办法来手动合并trace.dat If a crash happened on a ...
- Nginx-nginx和反向代理
使用版本:nginx-1.10.2(windows环境使用稳定版本) 下载地址:http://nginx.org 什么是nginx? Nginx (engine x) 是一款轻量级的Web 服务器 . ...
- arthas使用分享
文章目录准备启动Demo进入arthas控制台基础命令jvm相关class/classloader相关monitor/watch/trace相关options管道后台异步任务Web Console其他 ...
- C#WinForm应用程序中嵌入ECharts图表
C#WinForm应用程序中嵌入ECharts图表 程序运行效果: 下载ECharts: 官网下载ECharts :http://echarts.baidu.com/download.html 或者直 ...