题目描述:

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. ubuntu16.04 qt opencv3.4

    #------------------------------------------------- # # Project created by QtCreator 2018-12-12T14:53 ...

  2. MongoDB 那些坑

    MongoDB 是目前炙手可热的 NoSQL 文档型数据库,它提供的一些特性很棒:如自动 failover 机制,自动 sharding,无模式 schemaless,大部分情况下性能也很棒.但是薄荷 ...

  3. Python String模块详解

    >>> import string >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL ...

  4. Zookeeper使用--开源客户端

    一.ZkClient ZkClient是在Zookeeper原生API接口之上进行了包装,是一个更易用的Zookeeper客户端,其内部还实现了诸如Session超时重连.Watcher反复注册等功能 ...

  5. PythonQt第一例

    pythonQt第一例源码如下,主要介绍了简单的使用方式,需要注意的是应用程序的debug版本和release版本必须使用同类型的PythonQt库不可交叉使用. 源码地址:http://files. ...

  6. linux下安装或升级GCC4.8.2,以支持C++11标准[转]

    在编译kenlm的时候需要安装gcc, 然后还需要安装g++. g++安装命令:sudo apt-get install g++ ----------------------以下为网上转载内容,加上自 ...

  7. c#反射优化 表达式树

    using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...

  8. python学习的一点点心得

    好久没发博客了,不解释....接下来写一点自己最近学习python的一点心得. 想要学习python的初衷,是看<软件测试技术大全>一书时,了解到像perl.python.ruby等脚本类 ...

  9. java操作ceph之rbd基本操作

    一.安装librados和librbd Ceph存储集群提供了基本的存储服务,允许Ceph在一个统一的系统中唯一地传送对象,块和文件存储. 但是,不限于使用RESTful,块或POSIX接口. 基于R ...

  10. 在Linux上编译Hadoop-2.4.0

    目录 目录 1 1. 前言 1 2. 安装依赖 1 2.1. 安装ProtocolBuffer 2 2.2. 安装CMake 2 2.3. 安装JDK 2 2.4. 安装Maven 3 3. 编译Ha ...