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的更多相关文章

  1. 【Leetcode_easy】821. Shortest Distance to a Character

    problem 821. Shortest Distance to a Character solution1: class Solution { public: vector<int> ...

  2. 821. Shortest Distance to a Character - LeetCode

    Question 821. Shortest Distance to a Character Solution 思路:遍历字符串S,遇到与字符C相等就分别向左/右计算其他字符与该字符的距离,如果其他字 ...

  3. [Solution] 821. Shortest Distance to a Character

    Difficulty: Easy Problem Given a string S and a character C, return an array of integers representin ...

  4. LeetCode 821 Shortest Distance to a Character 解题报告

    题目要求 Given a string S and a character C, return an array of integers representing the shortest dista ...

  5. [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 ...

  6. 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...

  7. [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 ...

  8. [LeetCode] Shortest Distance to a Character 到字符的最短距离

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

  9. [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 ...

随机推荐

  1. vue-layer

    npm:  https://www.npmjs.com/package/vue-layer 原文:https://www.cnblogs.com/myIvan/p/9564502.html 1.安装 ...

  2. PLSQL连接Oracle数据库问题及详解

    一.Oracle数据库安装步骤参考:https://jingyan.baidu.com/article/363872eccfb9266e4aa16f5d.html 二.Oracle客户端安装:http ...

  3. bbs项目中对反向查询和分组查询的具体的应用

    我的数据库是按照下面的图片的方式设计的 然后看下model中代码 class User(models.Model): uid = models.AutoField(primary_key=True) ...

  4. cloud配置中心遇到的坑

    https://blog.csdn.net/z960339491/article/details/80593982分布式配置中心为什么要有用分布式配置中心这玩意儿?现在这微服务大军已经覆盖了各种大小型 ...

  5. iOS - iphoneX系列 - 全局配置的基本信息

      /// 获得当前窗口 var JY_WINDOW: UIWindow? { get{ if let app = UIApplication.shared.delegate as? AppDeleg ...

  6. ABAP空格和零的问题

    空格 concatenate wa_detail-zz_mark1 '' '' '' wa_detail-kdmat1 into wa_detail-zz_mark1 separated by spa ...

  7. netty 之 传统的阻塞io 体系回顾

    io 体系回顾: 流的概念: java 程序 通过流来进行完成输入与输出的.流是生产或者消费信息的抽象,流通过java 的 输入/输出系统与物理设备相连,尽管与他们链接的物理设备不尽相同,所有流的行为 ...

  8. ios 获取当前时间

    1.第一种返回的时间是一个整个的字符串. NSDate *timeDate = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateForm ...

  9. JS获取Dropdownlist选中值

    var dropDownList = document.getElementById("ddl_sheng"); //获取DropDownList控件 var dropDownLi ...

  10. c# txt内存映射技术总结

    对于大文件操作,readline 的方式读取文档,那操作起来跟蜗牛爬一样的慢了, 于是使用内存映射技术, 参考微软的这个使用方法说明 https://msdn.microsoft.com/zh-cn/ ...