• 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:

  1. S string length is in [1, 10000].
  2. C is a single character, and guaranteed to be in string S.
  3. All letters in S and C 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的更多相关文章

  1. 【Leetcode_easy】821. Shortest Distance to a Character

    problem 821. Shortest Distance to a Character solution1: class Solution { public: vector<int> ...

  2. 821. Shortest Distance to a Character - LeetCode

    Question 821. Shortest Distance to a Character Solution 思路:遍历字符串S,遇到与字符C相等就分别向左/右计算其他字符与该字符的距离,如果其他字 ...

  3. LeetCode 821 Shortest Distance to a Character 解题报告

    题目要求 Given a string S and a character C, return an array of integers representing the shortest dista ...

  4. [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 ...

  5. 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...

  6. 821. Shortest Distance to a Character

    class Solution { public: vector<int> shortestToChar(string S, char C) { int len=S.length(); ve ...

  7. [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 ...

  8. [LeetCode] Shortest Distance to a Character 到字符的最短距离

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

  9. [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 ...

随机推荐

  1. ThinkPHP3.2 中空方法、空控制器和空模块的设置

    ThinkPHP3.2 中空方法.空控制器和空模块的设置 1.空方法设置 问题: 当你访问一个不存在的方法的时候: 如: http://localhost/test/index.php/Home/Us ...

  2. [蓝桥杯]PREV-22.历届试题_国王的烦恼

    问题描述 C国由n个小岛组成,为了方便小岛之间联络,C国在小岛间建立了m座大桥,每座大桥连接两座小岛.两个小岛间可能存在多座桥连接.然而,由于海水冲刷,有一些大桥面临着不能使用的危险. 如果两个小岛间 ...

  3. 2016310Exp4 恶意代码及分析

    网络对抗 Exp4 恶意代码分析 实验内容 系统运行监控 恶意软件分析 报告评分 基础问题回答 实践目标 实验内容 1. 系统运行监控——计划任务 2. 系统运行监控——利用Sysmon 3.1恶意软 ...

  4. 20164310Exp1 PC平台逆向破解和BOF基础

    1.逆向及Bof基础实践说明        1.1实践目标 实践对象:pwn1的linux可执行文件 实践目的:使程序执行另一个代码(ShellCode) 实践内容: 手工修改可执行文件,改变程序执行 ...

  5. hibernate left join fetch 出错的问题

    1.首先说说manyToOne的问题 比如一个用户所在的组织机构,可能是多个,最多是四个,然后userEntity有下的代码: 关联查询: 第一种方式:代码如下 StringBuilder sql = ...

  6. [UE4]判断2个向量是否相等

    一.因为向量是3个浮点数,如果不使用误差的话,计算机里面的浮点数是有一定误差的. 二.如上图所示,如果2个向量的误差1厘米(UE4的单位是厘米),则表示2个向量相等.

  7. 微软 workflow 工作流总结2

    1.公共的状态机工作流 书签的设置 可以在判断模块中的action中赋值,因为在action中肯定要进入到下一个书签,所以可以在此给书签name赋值

  8. day1--一个简单的登录接口

    _usrename = "Yvan"_password = "abc123"count=0while count <3:    username = in ...

  9. element-ui-verify使用

    element-ui-verify是对ElementUI原本的校验封装之后的插件,并不会影响使用ElementUI的原生校验. 使用环境为vue+element-ui+webpack模块环境,首先使用 ...

  10. Navicat premium 12破解版

    下载Navicat  Premium 12和破解补丁Navicat_Keygen_Patch,底部有下载地址.下载之后安装Navicat,安装成功后先不要打开,然后打开破解补丁,破解补丁不需要安装,双 ...