821. Shortest Distance to a Character
2024-10-19 04:27:40
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;
}
};
最新文章
- MVC-命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Html”(是否缺少程序集引用?)
- C# MVC jsonp初接触成功
- json与对象转化
- JavaWeb高级:Servlet源码分析
- jsp中自定义Taglib案例
- [Linux Tips] 1. 查看端口
- ANSI
- 10大iOS开发者最喜爱的类库
- 树莓派(Rospberry Pi B+)到货亲測
- jQuery 分步引导 插件
- MessageDigest简要
- Appium入门示例(Java)
- java多线程的(一)-之java线程的使用
- 【Unity Shaders】游戏性和画面特效——创建一个老电影式的画面特效
- go-mysql,一个易用的mysql接口框架实现
- 第七节:语法总结(1)(自动属性、out参数、对象初始化器、var和dynamic等)
- centos服务器监控 服务器虚拟机里面的客户端zabbix-agent安装
- SSH高级服务
- CF113D 高斯消元、dp
- Iris分类以及数组reshape想到的
热门文章
- LibreOJ 6282. 数列分块入门 6
- SQL Server中row_number的用法
- C++ 计算直线的交点数(动态规划)
- 数据节点-df 无法输出结果
- Mysql自定义函数functions时报错
- .NET通用工具——正则表达式
- LIS LCS 最长上升子序列 最长公共子序列 ...
- 认识Thymeleaf:简单表达式和标签 基础信息
- 1F - A+B for Input-Output Practice (III)
- stl中顺序性容器,关联容器两者粗略解释