leetcode-821-Shortest Distance to a Character
题目描述:
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.
要完成的函数:
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的更多相关文章
- LeetCode 821 Shortest Distance to a Character 解题报告
题目要求 Given a string S and a character C, return an array of integers representing the shortest dista ...
- 821. Shortest Distance to a Character - LeetCode
Question 821. Shortest Distance to a Character Solution 思路:遍历字符串S,遇到与字符C相等就分别向左/右计算其他字符与该字符的距离,如果其他字 ...
- 【Leetcode_easy】821. Shortest Distance to a Character
problem 821. Shortest Distance to a Character solution1: class Solution { public: vector<int> ...
- 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...
- [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 ...
- [Solution] 821. Shortest Distance to a Character
Difficulty: Easy Problem Given a string S and a character C, return an array of integers representin ...
- [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 ...
- 821. Shortest Distance to a Character
class Solution { public: vector<int> shortestToChar(string S, char C) { int len=S.length(); ve ...
- [LeetCode] Shortest Distance to a Character 到字符的最短距离
Given a string S and a character C, return an array of integers representing the shortest distance f ...
- [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 ...
随机推荐
- PHP发红包程序限制红包的大小
我们先来分析下规律. 设定总金额为10元,有N个人随机领取: N=1 第一个 则红包金额=X元: N=2 第二个 为保证第二个红包可以正常发出,第一个红包金额=0.01至9.99之间的某个随机数. 第 ...
- Oracle——约束
NOT NULLUNIQUE PRIMARY KEYFOREIGN KEYCHECK 如果不指定约束名 ,Oracle server 自动按照 SYS_Cn 的格式指定约束名 --指定约束名 CREA ...
- SDN网络工具
TcpDump 根据使用者的定义对网络上的数据包进行截获的包分析工具. http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html ...
- java中List的用法和实例详解
java中List的用法和实例详解 List的用法List包括List接口以及List接口的所有实现类.因为List接口实现了Collection接口,所以List接口拥有Collection接口提供 ...
- 实践作业3:白盒测试----junit的难点DAY11.
本次白盒测试 需要独立完成整个项目和工具的配置安装运行操作,并编写.运行测试脚本,并完成实验的一些小细节等等. 首先,导入Junit测试框架所需的Jar包 然后编写测试脚本,为.java运行程序,见打 ...
- xfce4快捷键设置
xfce4的"Keyboard"可以方便的设置启动应用程序的快捷键. 例如,添加xfce4-terminal和emacs的启动快捷键 Alt+F3打开"Applicati ...
- ACM 媛在努力 华山论剑
媛在努力 描述在多媒体数据处理中,数据压缩算法尤为重要.小媛上完课后就想自己发明一个数据压缩算法.她想呀想,终于想到一个方法.在多媒体数据中有很多数据都是重复的,所以她想把连续相同的数据用数据出现的次 ...
- js链式调用
我们都很熟悉jQuery了,只能jQuery中一种非常牛逼的写法叫链式操作 * $('#div').css('background','#ccc').removeClass('box').stop() ...
- Jquery ajax的参数格式化
jQuery的ajax会自动将js对象转换为可传递的参数,$.param(jsobj, boolean),但是默认会把对象中数组类型加上[]符号,后台就不怎么好取了 参数boolean为true时不加 ...
- ibatis 参数之模糊查询
因项目需要最近使用ibatis,在使用查询语句的时候,想着通用性所以没有在配置文件里用N多的and 语句,而是如下: <select id="getUsersList" re ...