[Solution] 821. Shortest Distance to a Character
- Difficulty: Easy
Problem
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.
Solution
找到 S
中第一个 C
的位置,然后从这里往两边扩展距离,然后找到下一个 C
的位置,以此类推,直到遍历完 S
中所有的 C
。
实际上不难看出,位于 C
左边的字符,其距离计算只需进行一次(任意一个字符到 C
的距离,肯定比它到下一个 C
的距离更短),故向左扩展距离的时候就没必要扩展到字符串开头的位置。
public class Solution
{
public int[] ShortestToChar(string S, char C)
{
int[] ret = new int[S.Length];
int left = 0, right = S.Length, distance;
int startIndex = S.IndexOf(C, left);
Array.Fill(ret, 65535);
while(startIndex != -1)
{
distance = 0;
for(int i = startIndex; i >= left; i--)
{
if (ret[i] >= distance)
ret[i] = distance;
distance++;
}
distance = 0;
for(int i = startIndex; i < right; i++)
{
if (ret[i] >= distance)
ret[i] = distance;
distance++;
}
left = startIndex;
// 此处注意,查找下一个 C 的位置时,要排除掉当前的 C
// 否则程序会陷入死循环
startIndex = S.IndexOf(C, left + 1);
}
return ret;
}
}
提交后在 LeetCode 榜单上发现另外一个解法
public class Solution
{
public int[] ShortestToChar(string S, char C)
{
var len = S.Length;
var retval = new int[len];
var lastIdx = -len;
for (int m = 0; m < len; m++)
{
if (S[m] == C) lastIdx = m;
retval[m] = m - lastIdx;
}
lastIdx = len * 2;
for (int m = len - 1; m >= 0; m--)
{
if (S[m] == C) lastIdx = m;
retval[m] = Math.Min(lastIdx - m, retval[m]);
}
return retval;
}
}
[Solution] 821. Shortest Distance to a Character的更多相关文章
- 【Leetcode_easy】821. Shortest Distance to a Character
problem 821. Shortest Distance to a Character solution1: class Solution { public: vector<int> ...
- 821. Shortest Distance to a Character - LeetCode
Question 821. Shortest Distance to a Character Solution 思路:遍历字符串S,遇到与字符C相等就分别向左/右计算其他字符与该字符的距离,如果其他字 ...
- 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&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 ...
- 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...
- 821. Shortest Distance to a Character
class Solution { public: vector<int> shortestToChar(string S, char C) { int len=S.length(); ve ...
- [Swift]LeetCode821. 字符的最短距离 | Shortest Distance to a Character
Given a string S and a character C, return an array of integers representing the shortest distance f ...
- [LeetCode] Shortest Distance to a Character 到字符的最短距离
Given a string S and a character C, return an array of integers representing the shortest distance f ...
- [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 ...
随机推荐
- Git使用,将本地项目推送到GitHub上
首先本地仓库中创建一个项目 ex: proA 在远程github仓库中创建项目 ex: proA 在本地仓库proA下打开terminal 使用命令: 1.git add * 2.git commit ...
- bzoj5107: [CodePlus2017]找爸爸
Description 小A最近一直在找自己的爸爸,用什么办法呢,就是DNA比对.小A有一套自己的DNA序列比较方法,其最终目标是最 大化两个DNA序列的相似程度,具体步骤如下:1.给出两个DNA序列 ...
- HUE安装过程中的一些坑
1. gcc: error: krb5-config:: No such file or directory 执行安装krb5-devel yum provides krb5-config 得到提示: ...
- SSO单点登录三种情况的实现方式详解
单点登录(SSO——Single Sign On)对于我们来说已经不陌生了.对于大型系统来说使用单点登录可以减少用户很多的麻烦.就拿百度来说吧,百度下面有很多的子系统——百度经验.百度知道.百度文库等 ...
- 基于STM8的IIC协议---STM8-第五章
1. 综述 I2C(IIC,Inter-Integrated Circuit),两线式串行总线,由PHILIPS公司开发用于连接微控制器及其外围设备. 它是由数据线SDA和时钟SCL构成的串行总线,可 ...
- Django框架之中间件与Auth
Django框架之中间件与Auth模块一 cbv加装饰器 -先导入:from django.utils.decorators import method_decorator -1 可以在方法上加装饰器 ...
- python对mysql进行简单操作
python 连接MySQL数据库,进行简单操作 一.连接MySQL数据库,关闭连接 import pymysql db = pymysql.connect(host="xxx.xxx.x. ...
- mysql中的int和tinyint、varchar和char、DateTime和TimeStamp区别
一.int和tinyint的区别 大小: tinyint在mysql中占用1个字节 即: 1 bytes = 8 bit ,一个字节最多可以代表的数据长度是2的8次方:11111111 = 256 在 ...
- 另类P、V操作问题-详细图解
问题模型 有一个系统,定义如下P.V操作: P(s): s.count--; then 将本进程插入相应队列末尾等待; V(s): s.count++; then 从相应等待队列队尾唤醒一个进程,将其 ...
- uva-757-贪心
题意:有个人要去湖里钓鱼,总共有N个湖,排成一个序列,用字母P表示湖,从湖pi 到 pi+1(下一个湖)需要ti个五分钟. 并且,每个湖里可钓出来的鱼每过五分钟就减少di.如果产出的鱼小于等于di.那 ...