problem

821. Shortest Distance to a Character

solution1:

class Solution {
public:
vector<int> shortestToChar(string S, char C) {
int n = S.size();
vector<int> res(n, n);
for(int i=; i<n; ++i) if(S[i]==C) res[i] = ;
for(int i=; i<n; ++i) res[i] = min(res[i], abs(res[i-]+));
for(int i=n-; i>=; --i) res[i] = min(res[i], abs(res[i+]+));
return res;
}
};

solution2:

class Solution {
public:
vector<int> shortestToChar(string S, char C) {
int n = S.size();
vector<int> res(n, n);
int pos = -n;//errr...
for(int i=; i<n; ++i)
{
if(S[i]==C) pos = i;
res[i] = min(res[i], abs(i-pos));
}
for(int i=n-; i>=; --i)
{
if(S[i]==C) pos = i;
res[i] = min(res[i], abs(i-pos));
}
return res;
}
};

参考

1. Leetcode_easy_821. Shortest Distance to a Character;

2. Discuss;

3. grandyang;

【Leetcode_easy】821. Shortest Distance to a Character的更多相关文章

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

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

  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】1182. Shortest Distance to Target Color 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+二分查找 日期 题目地址:https://lee ...

  7. 【Leetcode_easy】849. Maximize Distance to Closest Person

    problem 849. Maximize Distance to Closest Person solution1: class Solution { public: int maxDistToCl ...

  8. 【Leetcode_easy】783. Minimum Distance Between BST Nodes

    problem 783. Minimum Distance Between BST Nodes 参考 1. Leetcode_easy_783. Minimum Distance Between BS ...

  9. 【Leetcode_easy】748. Shortest Completing Word

    problem 748. Shortest Completing Word 题意: solution1: class Solution { public: string shortestComplet ...

随机推荐

  1. Oracle之约束

    数据的完整性用于确保数据库数据遵从一定的商业的逻辑规则.在oracle中,数据完整性可以使用约束.触发器.应用程序(过程.函数)三种方法来实现,在这三种方法中,因为约束易于维护,并且具有最好的性能,所 ...

  2. vue 监听子组件事件及组件上使用v-model

  3. vscode 输出面板字符编码问题

    默认的输出中文会显示成乱码,需要在vscode内部的终端中输入 chcp 65001 缺点是需要每次打开vscode进行激活,另一种方式是在vscode的首选项中进行配置: "termina ...

  4. mysql - 引擎与锁的概念( 基础 )

    MySQL - 关系型数据库  - innodb : - 支持事务 事务的特征 : - 原子性:事务是最小单位,不可再分,事务执行的过程中,要么同时失败,要么同时成功,如,A跟B转账,一旦有一方出问题 ...

  5. yum安装出现No package crontabs available解决办法

    其意思是:yum中不存在这个包 所以解决办法是 1.更新yum   更新yum仓库: yum -y update 2.查看包名在yum中是什么   yum search  all crontabs

  6. 如何让MySQL语句执行加速?

    一打开科技类论坛,最常看到的文章主题就是MySQL性能优化了,为什么要优化呢? 因为: 数据库出现瓶颈,系统的吞吐量出现访问速度慢 随着应用程序的运行,数据库的中的数据会越来越多,处理时间变长 数据读 ...

  7. Nginx 负载均衡条件下 Tomcat 共享Session (Java)(一)

    1.修改tomcat 下 conf/context.xml  在</Context>里面加入以下代码 <Valve className="com.orangefunctio ...

  8. Kubernetes的YAML文件

    deployments: - apiVersion: apps/v1beta1 kind: Deployment metadata: labels: system_serviceUnit: bas-b ...

  9. 【luogu1251】餐巾计划问题--网络流建模,费用流

    题目描述 一个餐厅在相继的 N 天里,每天需用的餐巾数不尽相同.假设第 iii 天需要 ri​块餐巾( i=1,2,...,N).餐厅可以购买新的餐巾,每块餐巾的费用为 p 分;或者把旧餐巾送到快洗部 ...

  10. python 比较运算符和逻辑运算符

    <1> 比较(即关系)运算符 python中的比较运算符如下表 运算符 描述 示例 == 检查两个操作数的值是否相等,如果是则条件变为真. 如a=3,b=3则(a == b) 为 true ...