给定一个字符串 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. V8:V8(Javascript引擎)

    ylbtech-V8:V8(Javascript引擎) Lars Bak是这个项目的组长,目前该JavaScript引擎已用于其它项目的开发.第一个版本随着第一个版本的Chrome于2008年9月2日 ...

  2. R语言之数据处理

    R语言之数据处理 一.向量处理 1.选择和显示向量 data[1] data[3] data[1:3] data[-1]:除第一项以外的所有项 data[c(1,3,4,6)] data[data&g ...

  3. 洛谷P4244 [SHOI2008]仙人掌图 II

    传送门 首先不考虑带环的仙人掌,如果只是一棵普通的树,可以通过dp求每棵子树中的最长链和次长链求树的直径. 那么如果dfs的时候遇到了环,应该用环上的两点挂着的最长链加上两点间的距离来更新树的直径,并 ...

  4. 《数据结构与算法分析——C语言描述》ADT实现(NO.03) : 二叉搜索树/二叉查找树(Binary Search Tree)

    二叉搜索树(Binary Search Tree),又名二叉查找树.二叉排序树,是一种简单的二叉树.它的特点是每一个结点的左(右)子树各结点的元素一定小于(大于)该结点的元素.将该树用于查找时,由于二 ...

  5. css 始终显示滚动条,内容超出显示有滑块的滚动条,内容没有超出显示空的滚动条

    1.内容没有超出显示空的滚动条 <div class="div1"> 前端开发者前端开发者前端开发者前端开发者前端开发者 </div> css代码: .di ...

  6. Tomcat服务器的安装及配置

    学习目标: 了解Tomcat服务器的主要作用 掌握Tomcat服务器的安装与配置 掌握Tomcat安装目录下主要文件夹的作用 jsp的执行流程 1.Web的工作原理流程图:从图中可以看出Tomcat服 ...

  7. Ubuntu 18.04 美化

    Ubuntu 18.04 美化 sudo apt install gnome-tweak-tool sudo apt install gnome-shell-extensions sudo apt i ...

  8. vue/npm 错误提示&解决

    1,下图的报错为没有系统权限 解决: P.S. 用管理打开后的命令行默认是window/system32,从system32退回到桌面的路径如下图 2, vuex 报错 “unknown mutati ...

  9. [转]深入理解定位父级offsetParent及偏移大小

    偏移量(offset dimension)是javascript中的一个重要的概念.涉及到偏移量的主要是offsetLeft.offsetTop.offsetHeight.offsetWidth这四个 ...

  10. IO流3 --- File类的常用方法2 --- 技术搬运工(尚硅谷)

    File类的判断功能 @Test public void test5(){ File file = new File("hello.txt"); //判断是否是文件目录 Syste ...