• 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. python用字符串调用当前模块内的函数

    eval(字符串)() vars()[字符串]() 例如:

  2. GNU make 汇总

    = 是最基本的赋值 := 是覆盖之前的值?= 是如果没有被赋值过就赋予等号后面的值+= 是添加等号后面的值 $@--目标文件,$^--所有的依赖文件,$<--第一个依赖文件 makefile获取 ...

  3. 前后台数据交换,printwriter、jsonobject、jsonarray、ajax请求,数据交换

    后台代码: public void findByIDEquipment() { getResponse().setCharacterEncoding("UTF-8"); getRe ...

  4. ArcGIS紧凑型缓存存储格式分析

    by 蔡建良 2018-8-24 网络中我看到的网文将bundle存储切片数据的方式都没说清或是说错.按照错误方法一样可以在桌面浏览,但在arcgis for android却无法浏览. bundlx ...

  5. About Gnu Linker2

    3.5.1 Simple Assignments symbol = expression ; symbol += expression ; The first case will define sym ...

  6. connected standby

    参考链接 https://docs.microsoft.com/en-us/windows-hardware/design/device-experiences/modern-standby http ...

  7. git学习手记(也许仅对本人有用)

    首先明白git的三种状态 commited已提交 =====>git仓库(存着各种版本)modified已修改(此时就是我们的编辑器中的未保存状态)====>工作目录staged暂存状态= ...

  8. idea基本使用1

    首先推荐两个快捷键 alt+Ent        相当于eclipse中的crtl+1 alt+ins         :能创建包,类等,还能生成getter,setter,和构造函数 首先创建一个w ...

  9. ie浏览器许多图片放在一起会有间隙

    解决方法一(推荐):设置图片父元素font-size:0. 解决方法二:设置图片为float:并且图片设为块级元素.

  10. java.lang.Long 类源码解读

    总体阅读了Long的源码,基本跟Integer类类似,所以特别全部贴出源码,直接注释进行理解. // final修饰符 public final class Long extends Number i ...