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. POJ-3279.Fliptile(二进制状态压缩 + dfs) 子集生成

    昨天晚上12点刷到的这个题,一开始一位是BFS,但是一直没有思路.后来推了一下发现只需要依次枚举第一行的所有翻转状态然后再对每个情况的其它田地翻转进行暴力dfs就可以,但是由于二进制压缩学的不是很透, ...

  2. RxJS之AsyncSubject

    AsyncSubject 是另一个 Subject 变体,只有当 Observable 执行完成时(执行 complete()),它才会将执行的最后一个值发送给观察者. import { Compon ...

  3. Angular之替换根组件

    一 在index.html中,替换根组件选择器 <!doctype html> <html lang="en"> <head> <meta ...

  4. TZOJ 3481 Highway Construction(树的直径+最短路)

    描述 As head of the Accessible Commuting Movement (ACM), you've been lobbying the mayor to build a new ...

  5. c#: Noto Sans字体如何支持韩文

    1.源起: VCU10项目,使用了Noto Sans字体,的确漂亮.但验证在win7下,其显示韩文为乱码,颇为头痛. 其界面显示如图: 度娘之,得Noto Sans又有CJK字体,顾名思义,其为支持中 ...

  6. Day 04 列表,元祖,range

    列表: why: 1.字符串取值费劲 2.对字符串做任何操作,取出来的都是字符串 3.字符串有长度限制 基于以上原因,python提供了另一个数据类型,list 容器类数据类型. 列表页脚数组,可以存 ...

  7. 检测Android手机的IP地址

    package com.jason.demo.androidip; import android.content.Context; import android.net.DhcpInfo; impor ...

  8. illegal multibyte sequence python3

    错误信息如下: page_data = m.decode('gbk') UnicodeDecodeError: 'gbk' codec can't decode byte 0xa7 in positi ...

  9. .net项目错误:找不到方法:“System.Net.Http.HttpClient stellar_dotnet_sdk.Server.get_HttpClient()

    1.由于在项目里面引用了一个 新的项目stellar_dotnet_sdk,在  return new StellarWallet(ConvertToWalletSetting(coin));   的 ...

  10. OpenSource.SerializationLibrary

    1. Cap'n Proto protocol buffer的主要作者之一创建的新项目.其主页描述Cap'n Proto的性能比PB快很多. http://kentonv.github.io/capn ...