给定一个字符串 S 和一个字符 C。返回一个代表字符串 S 中每个字符到字符串 S 中的字符 C 的最短距离的数组。

示例 1:

输入: S = "loveleetcode", C = 'e' 输出: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]

说明:

  1. 字符串 S 的长度范围为 [1, 10000]。
  2. C 是一个单字符,且保证是字符串 S 里的字符。
  3. S 和 C 中的所有字母均为小写字母。
class Solution {
public:
vector<int> shortestToChar(string S, char C) {
vector<int> res;
int len = S.size();
for(int i = 0; i < len; i++)
{
if(S[i] == C)
res.push_back(0);
else
{
int j;
for(j = i; j < len; j++)
{
if(S[j] == C)
{
break;
}
}
int k;
for(k = i; k >= 0; k--)
{
if(S[k] == C)
{
break;
}
}
if(j >= len)
{
res.push_back(abs(i - k));
}
else if(k < 0)
{
res.push_back(abs(i - j));
}
else
res.push_back(min(abs(i - j), abs(i - k)));
}
}
return res;
}
};

Leetcode821.Shortest Distance to a Character字符的最短距离的更多相关文章

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

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

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

  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】821. Shortest Distance to a Character 解题报告(Python)

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

  6. 821. Shortest Distance to a Character - LeetCode

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

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

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

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

  9. [LeetCode] 317. Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

随机推荐

  1. 工控安全入门(六)——逆向角度看Vxworks

    上一篇文章中我们对于固件进行了简单的分析,这一篇我们将会补充一些Vxworks的知识,同时继续升入研究固件内容. 由于涉及到操作系统的内容,建议大家在阅读本篇前有一定操作系统知识的基础,或者是阅读我的 ...

  2. KOA 学习(八) koa-bodyparser

    此控件支持Josn,form,text类型 用法 var Koa = require('koa'); var bodyParser = require('koa-bodyparser'); var a ...

  3. python-web-webbrower-beautifuSoup

    webbrowser:是 Python 自带的,打开浏览器获取指定页面. requests:从因特网上下载文件和网页. Beautiful Soup:解析 HTML,即网页编写的格式. seleniu ...

  4. HDFS 数据错误与恢复

  5. Leetcode89. Gray Code格雷编码

    给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 示例 1: 输入: 2 输出: [0,1,3,2] 解释: 00 - 0 01 - 1 11 - 3 10 - ...

  6. Django项目:CRM(客户关系管理系统)--27--19PerfectCRM实现King_admin数据修改

    登陆密码设置参考 http://www.cnblogs.com/ujq3/p/8553784.html {#table_data_list.html#} {## ————————08PerfectCR ...

  7. postman发送get和post请求

    一.postman发送get请求   在地址栏里输入请求url(用到拼接方式):http://127.0.0.1:8081/getuser?userid=1 选择“GET”方式, 点击“send”得到 ...

  8. python 内置操作函数

  9. NOIP模拟 7.01

    水灾(sliker.cpp/c/pas) 1000MS  64MB 大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没. CCY ...

  10. BZOJ1452 [JSOI2009]Count [2017年4月计划 树状数组02]

    1452: [JSOI2009]Count Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2419  Solved: 1403[Submit][Stat ...