[LeetCode] 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.
这道题给了我们一个字符串S,和一个字符C,让我们求字符串中每个字符到字符C到最短距离,这里的字符C可能有多个。题目中给的例子中就是有多个e,每个e的位置一定是0,其他位置的值是到其最近的e的距离。最原始粗犷的方法肯定是对于每个非e的字符,左右两个方向来查找e,令博主感到意外的是,这种暴力搜索的方法居然可以通过OJ,太仁慈了啊,参见代码如下:
解法一:
class Solution {
public:
vector<int> shortestToChar(string S, char C) {
int n = S.size();
vector<int> res(n, n);
for (int i = ; i < n; ++i) {
if (S[i] == C) {res[i] = ; continue;}
int j = i + ;
while (j < n && S[j] != C) ++j;
if (j < n) res[i] = j - i;
j = i - ;
while (j >= && S[j] != C) --j;
if (j >= ) res[i] = min(res[i], i - j);
}
return res;
}
};
一般来说,优化线性搜索的方法就是用二分搜索法,这里我们先把所有为字符C的位置存入数组idx,因为是按顺序遍历的,所以idx数组也是有序的,这为二分搜索创造了条件。然后对于数组中的每一个位置,我们都在idx数组中二分查找不小于该位置的数,这时候要分情况讨论一下,如果找不到这样的数的时候,说明所有字符C的位置都在当前位置的左边,那么我们取idx数组中最后一个数,就是左边最近的一个字符C,求出距离即可。如果返回的是idx数组中的首数字,说明当前的位置是字符C,或者最近的字符C在右边,那么只要用这个首数字减去当前位置就是最近距离了。对于其他情况,左右两边都有字符C,所以我们都要各自计算一下距离,然后取较小的那个即可,参见代码如下:
解法二:
class Solution {
public:
vector<int> shortestToChar(string S, char C) {
vector<int> res, idx;
for (int i = ; i < S.size(); ++i) {
if (S[i] == C) idx.push_back(i);
}
for (int i = ; i < S.size(); ++i) {
auto it = lower_bound(idx.begin(), idx.end(), i);
if (it == idx.end()) res.push_back(i - *(--it));
else if (it == idx.begin()) res.push_back(*it - i);
else {
int d1 = *it - i, d2 = i - *(--it);
res.push_back(min(d1, d2));
}
}
return res;
}
};
还有一种类似距离场的解法,与解法一不同的是,这里是对于每个是字符C的位置,然后分别像左右两边扩散,不停是更新距离,这样当所有的字符C的点都扩散完成之后,每个非字符C位置上的数字就是到字符C的最短距离了,参见代码如下:
解法三:
class Solution {
public:
vector<int> shortestToChar(string S, char C) {
int n = S.size();
vector<int> res(n, n);
for (int i = ; i < n; ++i) {
if (S[i] != C) continue;
res[i] = ;
for (int j = i + ; j < n && S[j] != C; ++j) {
res[j] = min(res[j], j - i);
}
for (int j = i - ; j >= && S[j] != C; --j) {
res[j] = min(res[j], i - j);
}
}
return res;
}
};
下面这种方法也是建立距离场的思路,不过更加巧妙一些,只需要正反两次遍历就行。首先进行正向遍历,若当前位置是字符C,那么直接赋0,否则看如果不是首位置,那么当前位置的值等于前一个位置的值加1。这里不用和当前的值进行比较,因为这个算出来的值不会大于初始化的值。然后再进行反向遍历,要从倒数第二个值开始往前遍历,用后一个值加1来更新当前位置的值,此时就要和当前值做比较,取较小的那个,参见代码如下:
解法四:
class Solution {
public:
vector<int> shortestToChar(string S, char C) {
vector<int> res(S.size(), S.size());
for (int i = ; i < S.size(); ++i) {
if (S[i] == C) res[i] = ;
else if (i > ) res[i] = res[i - ] + ;
}
for (int i = (int)S.size() - ; i >= ; --i) {
res[i] = min(res[i], res[i + ] + );
}
return res;
}
};
参考资料:
https://leetcode.com/problems/shortest-distance-to-a-character/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Shortest Distance to a Character 到字符的最短距离的更多相关文章
- [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 ...
- 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...
- 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&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_easy】821. Shortest Distance to a Character
problem 821. Shortest Distance to a Character solution1: class Solution { public: vector<int> ...
- LeetCode算法题-Shortest Distance to a Character(Java实现)
这是悦乐书的第321次更新,第343篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第191题(顺位题号是821).给定字符串S和字符C,返回一个整数数组,表示字符串中所有 ...
- Leetcode821.Shortest Distance to a Character字符的最短距离
给定一个字符串 S 和一个字符 C.返回一个代表字符串 S 中每个字符到字符串 S 中的字符 C 的最短距离的数组. 示例 1: 输入: S = "loveleetcode", C ...
随机推荐
- 第六节:反射(几种写法、好处和弊端、利用反射实现IOC)
一. 加载dll,读取相关信息 1. 加载程序集的三种方式 调用Assembly类下的三个方法:Load.LoadFile.LoadFrom. //1.1 Load方法:动态默认加载当前路径下的(bi ...
- Oracle使用PLSQL导入数据后中文乱码的解决方法
新建环境变量 名:NLS_LANG 值:SIMPLIFIE DCHINESE_CHINA.ZHS16GBK 保存后重启PLSQL Developer 重新导入. 如果还是乱码,将上面8的环境变量值改为 ...
- Maven安装及配置
第1部分 准备 1.1 安装JDK和Eclipse: 1.2 下载Maven(https://maven.apache.org/download.cgi) 第2部分 2.1 安装Maven 2.1.1 ...
- 在Unity3D里使用WinForm
之前给一个游戏写过MOD,功能大概是在游戏里可以打开一个编辑器,然后可以直接在编辑器里修改到游戏数据. 编辑器UI的实现部分,一开始用的是原生GUI,即OnGUI部分,这种方式虽然最简洁,也不用引用任 ...
- Django2.1,Xadmin2.0下的问题记录
此篇博文长期更新…… 环境: Ubuntu18.04, Python3.6, Django2.1, Xadmin2.0 1. Xadmin添加用户小组件时报错:xadmin render() got ...
- java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
♦ 问题所在:项目lib包里少一个jar包 ♦ 解决办法: commons-lang3-3.1.jar 导入到项目就ok
- LaTeX技巧892: Ubuntu 安装新版本TeXLive并更新
原文地址:http://www.latexstudio.net/archives/9788.html 摘要: 本文比较系统地介绍了在Ubuntu下的TeXLive的安装与配置测试过程,建议使用Ubun ...
- ActiveMQ依赖JDK版本关系
1.如何查看官方发布的activeMQ依赖的JDK版本1)以ActiveMQ 5.15.2 Release为例:在下载页面的Change Log处, 2)打开下载号的jar包,以activemq-al ...
- Easy RM to MP3 Converter栈溢出定位及漏洞利用
本文主要是Easy RM to MP3 Converter(MFC++编写)栈溢出的定位及windows下shellcode编写的一些心得. 用到的工具及漏洞程序下载地址https://github. ...
- 【原创】大叔经验分享(23)spark sql插入表时的文件个数研究
spark sql执行insert overwrite table时,写到新表或者新分区的文件个数,有可能是200个,也有可能是任意个,为什么会有这种差别? 首先看一下spark sql执行inser ...