[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:
Sstring length is in[1, 10000].Cis a single character, and guaranteed to be in stringS.- All letters in
SandCare 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 ...
随机推荐
- MySQL 索引的增删查
查看索引: > SHOW INDEX FROM table_name; > SHOW KEYS FROM table_name; 删除索引: > DROP INDEX index ...
- CentOS7.4下部署hadoop3.1.1
CentOS7.4下部署hadoop3.1.1 契机 由于工作原因要部署hadoop的集群,习惯使用最新的稳定版本2018年的时候由于时间紧破部署了2.7.2版本,最新由于又要部署有研究了一下3.x的 ...
- python学习笔记八(集合)
集合 set是一个无序的不重复元素序列. 用大括号或set()函数创建集合.注意:创建一个空集合用set(),不用{},{}用来创建空字典 basket = {'apple','orange','ap ...
- 3.Qt GUI中一些操作记录
一.如何在Widget中利用代码添加背景图片 this->setAutoFillBackground(true); // QPalette palette = this->palette( ...
- ARC085E MUL
https://atcoder.jp/contests/arc085/tasks/arc085_c 题目大意 略 解法 最小割即可. 直接建图有负边,但是因为我们知道最后在割上的边数一定为 \(N\) ...
- redis4.0.13主从、哨兵、集群3种模式的 Server端搭建、启动、验证
本文使用的是redis-4.0.13.tar.gz版本. 两个centos7系统虚拟机:192.168.10.140.192.168.10.150 redis各版本下载地址:http://downlo ...
- PAT 乙级 1044 火星数字 (20 分)
1044 火星数字 (20 分) 火星人是以 13 进制计数的: 地球人的 0 被火星人称为 tret. 地球人数字 1 到 12 的火星文分别为:jan, feb, mar, apr, may, j ...
- Struts S2-048 RCE漏洞分析
应该是S2-048目前最详细的一篇了.. 漏洞影响 Struts 2.3.x系列中的Showcase应用 使用了struts1 插件,并在使用ActionMessages时将客户端可控的参数拼接传递给 ...
- iPerf 笔记
iPerf 笔记 iperf 是一个网络性能测试工具,可以测试最大TCP和UDP带宽质量,具有多种参数和UDP特性,可以报告带宽.延迟抖动和数据包丢失. iperf 可以用来测一些网络设备,比如路由器 ...
- C#学习-图解教程(2):访问修饰符(其中两种)
学习内容:C#:学习书籍:图解教程(中文第四版). 目录:第四章 类的基本概念 -----> 4.8 访问修饰符 访问修饰符 从类的内部,任何函数成员都可以使用成员的名称访问类中任意的其他成员. ...