821. Shortest Distance to a Character
class Solution
{
public:
vector<int> shortestToChar(string S, char C)
{
int len=S.length();
vector<int>res(len,-); //as the result vector
vector<int> cindex; //save the index of C
for(int i=;i<len;i++) //write the distance of C and build the cindex
if(S[i]==C)
{
cindex.push_back(i);
res[i]=;
}
int szindex=cindex.size(); int count=;
for(int j=cindex[]-;j>=;j--) //write the distance before the first C
res[j]=count++; count=;
for(int k=cindex[szindex-]+;k<len;k++) //write the distance after the last C
res[k]=count++; for(int x=;x<szindex;x++) //write the distance between two C
{
count=;
int left=cindex[x-]+,right=cindex[x]-;
while(left<=right)
{
res[left++]=count;
res[right--]=count++;
}
}
return res;
}
};
821. Shortest Distance to a Character的更多相关文章
- 【Leetcode_easy】821. Shortest Distance to a Character
problem 821. Shortest Distance to a Character solution1: class Solution { public: vector<int> ...
- 821. Shortest Distance to a Character - LeetCode
Question 821. Shortest Distance to a Character Solution 思路:遍历字符串S,遇到与字符C相等就分别向左/右计算其他字符与该字符的距离,如果其他字 ...
- [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 解题报告
题目要求 Given a string S and a character C, return an array of integers representing the shortest dista ...
- [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 ...
- 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...
- [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] 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_Easy tag: BFS
Given a string S and a character C, return an array of integers representing the shortest distance f ...
随机推荐
- poj2970 The lazy programmer 【优先队列】
A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is ...
- Linux系统清除缓存
1)缓存机制介绍在Linux系统中,为了提高文件系统性能,内核利用一部分物理内存分配出缓冲区,用于缓存系统操作和数据文件,当内核收到读写的请求时,内核先去缓存区找是否有请求的数据,有就直接返回,如果没 ...
- CentOS 下lvm 磁盘扩容
打算给系统装一个oracle,发现磁盘空间不足.在安装系统的时候我选择的是自动分区,系统就会自动以LVM的方式分区.为了保证系统后期的可用性,建议所有新系统安装都采用LVM,之后生产上的设备我也打算这 ...
- Flask中路由系统、Flask的参数及app的配置
@app.route('/', methods=['GET', 'POST']) 1. @app.route()装饰器中的参数 methods:当前URL地址,允许访问的请求方式 @app.route ...
- python之集合【set】
初学python,今天晚上学习了set,做下set的总结,set的中文名也就是[集合],set的总结分为两部分,第一部分是set的创建,第二部分是set的操作,也就是set的功能:set的特点是无序的 ...
- linux命令学习之:curl
curl命令是一个利用URL规则在命令行下工作的开源文件传输工具.它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具.作为一款强力工具,curl支持包括HTTP.HTTPS ...
- sharpsvn 继续,解决文件locked 问题,
方法中少个方法就会出现一些问题. 比如进行了断线测试,结果再操作时就出现了文件被锁的情况,最终查了官网的论坛,才得以解决 How to unlock if the working copy is lo ...
- js 逻辑运算符
两个逻辑运算符的操作顺序在自己的脑海里一直理不清,用js做了个实验 <script type="text/javascript"> if(false && ...
- 使用GrabCut提取前景图像的示范代码
#include<opencv2/opencv.hpp> bool selectObject = false; cv::Point origin; cv::Rect selection; ...
- Centos7上卸载openJdk安装,安装自己的JDK1.8
1.下载jdk-1.8,官网下载1.8 注意:不要使用wget命令去下载jdk,若能够下载下来,解压的时候也会报错的: 2.卸载openjdk (1)使用 rpm -qa | grep java ...