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 ...
随机推荐
- JavaWeb网站后台开发记录手册
1.javaweb网站后台开发 1.封装DBTools类 1.注册数据库驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); 2 ...
- Codeforces Beta Round #73 (Div. 2 Only)
Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...
- Angular之替换根组件
一 在index.html中,替换根组件选择器 <!doctype html> <html lang="en"> <head> <meta ...
- TZOJ 2415 Arctic Network(最小生成树第k小边)
描述 The Department of National Defence (DND) wishes to connect several northern outposts by a wireles ...
- f5 2017.09.03故障
1.下午14点50左右有同事反应epm等系统登录有问题.自测登录也是有同样的报错. 2.测试发现内部IP直接访问正常,但是访问f5的vip的方式访问不了.此时oa.邮件等系统也开始有同事发现故障. 3 ...
- C++中的字符数组与字符指针
//[C++基础]字符数组和字符指针.cpp//剑指offer上的这段话://为了节省内存,c/c++把常量字符串放到单独的一个内存空间.但是当几个指针赋值给相同的常量字符串时,它们实际上会指向相同的 ...
- 点线特征双目视觉SLAM---暑期笔记
1.由于以后可能研究有关基于特征方面的SLAM研究,所以近期看了一篇文章[基于点线综合特征的双目视觉SLAM方法--谢晓佳],由于之前对SLAM的模块比较模糊,所以认真阅读了此论文,并对主要的3个线程 ...
- [z]规则引擎
https://www.ibm.com/developerworks/cn/java/j-drools/ 使用声明性编程方法编写程序的业务逻辑 使用规则引擎可以通过降低实现复杂业务逻辑的组件的复杂性, ...
- ContenteProvider
以前只写过程序中添加背景音乐,在程序一开始就运行音乐,当程序结束后音乐也随即停止.遇到这样的功能,我们一般是通过系统提供的ContentProvider来实现的,系统对于常用的数据也给开发者提供了方便 ...
- python 3.6.5 map() max() lambda匿名函数
python 3.6.5 sample: map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 functi ...