题目描述:

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.

要完成的函数:

vector<int> shortestToChar(string S, char C)

说明:

1、给定一个字符串S和字符C,找到字符串S中字符C的位置(可能有多个字符C),返回字符串S中所有字符距离最近的字符C的距离。

比如S为leetcode,C为e,那么返回的距离vector就是[3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]。

2、这道题题意很清晰,也有点像之前做过的一道题,笔者在这里模仿那道题的做法。

给一个例子说明一下,比如S为leetcodell,C为e。

那么我们先把每个字符都跟它右边或者本身的e比较距离,如果右边没有e了,那么插入INT_MAX。

遍历一遍字符串S,我们可以得到[1,0,0,4,3,2,1,0,INT_MAX,INT_MAX]。

接着我们再把每个字符都跟它左边或者本身的e比较距离,这个距离跟上述得到的距离,取一个最小值。如果左边没有e了,那么不做处理。

遍历一遍字符串S,最后得到的距离vector就是我们要的了。

上述做法,其实是把每个左边有e右边也有e的字符,计算一下距离左边的距离,再计算一下距离右边的距离,然后取一个最小值。

对于那些只有右边有e的字符,只处理一次,对于只有左边有e的字符,也只处理一次。

代码实现如下(附详解),分享给大家:

    vector<int> shortestToChar(string S, char C)
{
vector<int>index;//记录S中C的位置
vector<int>res;//最后要返回的距离vector
int s1=S.size();
for(int i=0;i<s1;i++)//不断插入C的位置
{
if(S[i]==C)
index.push_back(i);
}
int i=0,j=0,s2=index.size();
while(i<s1)//计算每个字符跟右边C的距离
{
if(j<s2)//如果右边有C
{
if(i<index[j])
{
res.push_back(index[j]-i);
i++;
}
else if((i==index[j]))
{
res.push_back(0);
j++;
i++;
}
}
else//如果右边没有C,那么插入INT_MAX
{
res.push_back(INT_MAX);
i++;
}
}
i=s1-1,j=s2-1;
while(j>=0)//如果左边有C,计算每个字符跟左边C的距离
{
if(i>index[j])
{
res[i]=min(res[i],i-index[j]);//只保留最小值
i--;
}
else if((i==index[j]))
{
j--;
i--;
}
}
return res;
}

上述代码实测15ms,因为服务器接受到的cpp submissions有限,所以没有打败的百分比。

leetcode-821-Shortest Distance to a Character的更多相关文章

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

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

  2. 821. Shortest Distance to a Character - LeetCode

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

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

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

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

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

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

  6. [Solution] 821. Shortest Distance to a Character

    Difficulty: Easy Problem Given a string S and a character C, return an array of integers representin ...

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

  8. 821. Shortest Distance to a Character

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

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

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

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

随机推荐

  1. Red Hat Cluster Suite 组件 fencing FAQ

    说明 Red Hat Cluster实现HA的关键组件之一是fencing.没有设置fencing,虽然看上去也能够运行Cluster,但是一旦遇到故障切换就会出现异 常,所以深入理解fencing原 ...

  2. 建立spring项目入门实例

    建立maven项目 打开pop.xml文件 添加springframework所依赖的包 <!-- https://mvnrepository.com/artifact/org.springfr ...

  3. Paradox

    克己博伦 当一个无法阻挡的力量,碰到了一个无法移动的物体?如果这个力量移动了物体,那么这个物体就不是无法移动的.如果这个力量没有移动物体,那么这个无法阻挡的力量就被挡了下来. 上帝能造出一个重到他自己 ...

  4. 区分 hdfs hbase hive hbase适用场景

    区分 hdfs hbase hive hbase适用场景 收藏 八戒_o 发表于 11个月前 阅读 308 收藏 1 点赞 0 评论 0 摘要: hdfs hbase hive hbase适用场景 H ...

  5. Ubuntu Phone开箱上手

    在昨晚举行的发布会上Canonical和硬件厂商BQ进行合作,推出了首款面向消费市场的Ubuntu手机--Aquaris E4.5,带来了与常见的iPhone和Android机完全不同的操作体验,设备 ...

  6. name相同获取值

    <html> <head> </head> <body> <form name="form1" method="po ...

  7. Flask框架 之 信号

    Flask框架中的信号基于blinker,其主要就是让开发者可是在flask请求过程中定制一些用户行为. 安装 pip3 install blinker 内置信号 request_started = ...

  8. Photo4

    Story: 我手捧玫瑰,一个人,走在桥上.桥下是波澜壮阔的大海,一不小心,我就有失足的危险.海鸟的低鸣在我耳际盘旋着,浪汹涌,仿佛要把我吞噬掉.你也许奇怪,为何我一人手捧玫瑰走在桥上.只因,女骑从来 ...

  9. AID-应用标识符的组成规则

    AID:即唯一标识一个应用,分为两部分,RID(5字节)+PIX(最多11字节) RID:注册标识符,由ISO组织来分配,标识一个全球唯一的应用提供商,一般是分配给卡组织,比如分配给Master,比如 ...

  10. Spring MVC 配置文件

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...