【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 ...
随机推荐
- Telink BLE MESH PWM波的小结
本本针对Telink BLE MESH SDK 灯控的使用进行说明. 1.调整灯光的频率 默认情况下 SDK PWM波的频率是 600HZ的,有时我们需要将它调整频率,例如调整为4K,只需要更改参数 ...
- 12 — springboot集成JPA — 更新完毕
1.什么是jpa? 一堆不想整在这博客里面的理论知识.这些理论玩意儿就应该自行领悟到自己脑海里 1).JPA & Spring Data JPA 1.1).JPA JPA是Java Persi ...
- 1小时学会Git玩转GitHub
版权声明:原创不易,本文禁止抄袭.转载,侵权必究! 本次教程建议一边阅读一边用电脑实操 目录 一.了解Git和Github 1.1 什么是Git 1.2 什么是版本控制系统 1.3 什么是Github ...
- 『学了就忘』Linux启动引导与修复 — 70、grub启动引导程序的配置文件说明
目录 1.grub中分区的表示方法 2.grub的配置文件 3.grub的配置文件内容说明 (1)grub的整体设置 (2)CentOS系统的启动设置 1.grub中分区的表示方法 在说grub启动引 ...
- Oracle中常用的系统函数
本文主要来梳理下Oracle中的常用的系统函数,掌握这些函数的使用,对于我们编写SQL语句或PL/SQL代码时很有帮助,所以这也是必须掌握的知识点. 本文主要包括以下函数介绍:1.字符串函数2. 数值 ...
- java设计模式—Decorator装饰者模式
一.装饰者模式 1.定义及作用 该模式以对客户端透明的方式扩展对象的功能. 2.涉及角色 抽象构件角色:定义一个抽象接口,来规范准备附加功能的类. 具体构件角色:将要被附加功能的类,实现抽象 ...
- shell神器curl命令的用法 curl用法实例笔记
shell神器curl命令的用法举例,如下: ##基本用法(配合sed/awk/grep) $curl http://www.jquerycn.cn ##下载保存 $curl http://www.j ...
- When do we pass arguments by reference or pointer?
在C++中,基于以下如下我们通过以引用reference的形式传递变量. (1)To modify local variables of the caller function A reference ...
- APiCloud - api对象
详见网址:https://www.cnblogs.com/jiqing9006/p/5919317.html
- Next_day()函数的用法
一.定义 NEXT_DAY(date,char) date参数为日期型, char:为1~7或Monday/Mon~Sunday/ 指定时间的下一个星期几(由char指定)所在的日期, c ...