【LeetCode】821. Shortest Distance to a Character 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/shortest-distance-to-a-character/description/
题目描述
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 string S.
- All letters in S and C are lowercase.
题目大意
给定字符串S和属于该字符串的一个字符C,要求出字符串中的每个字符到最近的C的距离。
解题方法
过两遍数组
这个解题方法,有点骚。属于两步走的方案:
第一步,先假设在很远的位置有个C字符,那么从左到右开始遍历,找出每个字符到其最近的左边的字符C的距离;
第二步,先假设在很远的位置有个C字符,那么从右到左开始遍历,找出每个字符到其最近的右边的字符C的距离,并和第一步求出的距离进行比较,找出最小值为结果;
两个技巧:
- 设了一个比字符串长度更远的一个字符C,保证后面求最小值更新距离时一定会被更新。
- 无论如何都用到了abs求绝对值,哪怕可能是不需要的,目的是不用费脑子思考谁大谁小了。
class Solution:
def shortestToChar(self, S, C):
"""
:type S: str
:type C: str
:rtype: List[int]
"""
_len = len(S)
index = -1000000
ans = [0] * _len
for i, s in enumerate(S):
if s == C:
index = i
ans[i] = abs(i - index)
index = -100000
for i in range(_len - 1, -1 , -1):
if S[i] == C:
index = i
ans[i] = min(abs(i - index), ans[i])
return ans
日期
2018 年 5 月 27 日 —— 周末的天气很好~
2018 年 11 月 6 日 —— 腰酸背痛要废了
【LeetCode】821. Shortest Distance to a Character 解题报告(Python)的更多相关文章
- LeetCode 821 Shortest Distance to a Character 解题报告
题目要求 Given a string S and a character C, return an array of integers representing the shortest dista ...
- 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】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
随机推荐
- Identity Server 4 从入门到落地(三)—— 创建Web客户端
书接上回,我们已经搭建好了基于Identity Server 4的认证服务和管理应用(如果还没有搭建,参看本系列前两部分,相关代码可以从github下载:https://github.com/zhen ...
- vue开发多页面应用 - hash模式和history模式
我们知道vue可以快速开发web单页应用,而且官方为我们提供了自己的应用脚手架vue-cli,我们只需要下载脚手架,安装依赖后就可以启动vue应用雏形. 这得益与webpack的依赖追踪,各种资源后缀 ...
- Tomcat源码分析 | 一文详解生命周期机制Lifecycle
目录 什么是Lifecycle? Lifecycle方法 LifecycleBase 增加.删除和获取监听器 init() start() stop() destroy() 模板方法 总结 前言 To ...
- oracle 外部表查alter日志
--创建文件夹,路径是alter日志的路径 create or replace directory data_dir as '/u01/app/oracle/diag/rdbms/orcl/orcl/ ...
- Mockito 入门详解
一个测试方法主要包括三部分: setup 执行操作 验证结果 public class CalculatorTest { Calculator mCalculator; @Before // setu ...
- window安装ab压力测试
ab是Apache HTTP server benchmarking tool的缩写,可以用以测试HTTP请求的服务器性能,也是业界比较流行和简单易用的一种压力测试工具包 ## 下载 下载地址:(ht ...
- feignclient发送get请求,传递参数为对象
feignclient发送get请求,传递参数为对象.此时不能使用在地址栏传递参数的方式,需要将参数放到请求体中. 第一步: 修改application.yml中配置feign发送请求使用apache ...
- AI 2021 年度报告
建议大伙有空还是自己亲自读一下,虽然有点长,188页ppt. https://docs.google.com/presentation/d/1bwJDRC777rAf00Drthi9yT2c9b0Ma ...
- 06 - Vue3 UI Framework - Dialog 组件
做完按钮之后,我们应该了解了遮罩层的概念,接下来我们来做 Dialog 组件! 返回阅读列表点击 这里 需求分析 默认是不可见的,在用户触发某个动作后变为可见 自带白板卡片,分为上中下三个区域,分别放 ...
- centos部署配置gogs代码仓库
目录 一.简介 二.部署 三.网页配置 一.简介 Gogs的目标是打造一个最简单.最快速和最轻松的方式搭建自助Git服务.使用Go语言开发使得Gogs能够通过独立的二进制分发,并且支持Go语言支持的 ...