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. Java虚拟机栈---本地方法栈

    1.Java虚拟机栈(Java Virtual Machine Stacks) 线程私有,它的生命周期与线程相同.描述的是Java方法执行的内存模型:每个方法在执行的同时都会创建一个栈帧(Stack ...

  2. 学到了林海峰,武沛齐讲的Day23-完

    10月11号生了儿子,很高心..不好的是孩子住院了,14号出院,晚上外公去世了,15号赶回老家.....20号回贵阳,21号回公司办事....我要坚定的学习下去...以前几乎是卡在这里就学不下去了.加 ...

  3. Spring 对事务的整合

    对事务的复习 什么是事务: 事务(TRANSACTION) 是作为单个逻辑工作单元执行的一系列操作. 多个操作作为一个整体向系统提交,要么都执行,要么都不执行. 事务是一个不可分割的逻辑单元. 事务的 ...

  4. java前后分离了 controller接口GET和POST

    通用 @RestController GET @RequestMapping("userbase/{userId}") public User selectById(@PathVa ...

  5. xlrd/xlwt

    操作 xls格式的excel文件 读模块 xlrd import xlrd 打开文件 wb= xlrd.open_workbook('xxxx.xls') 获取excel中的表 ws= wb.shee ...

  6. ubuntu16.04源码编译安装nginx1.14.2

    1.下载nginx-1.14.2, 官网地址:nginx.org 2.解压nginx-1.14.2.tar.gz tar zxvf nginx-1.14.2.tar.gz 3.切到文件夹nginx-1 ...

  7. Storage Port Drivers

    为了学习存储知识,我也是拼了,来,翻译一下下面这篇微软的文章(如果有谁翻译过了,或者微软有中文翻译,请绕路): Storage Port Drivers Last Updated: 4/20/2017 ...

  8. vscode搭建springboot开发环境

    1. JDK,Maven 1.1 下载略 1.2 设置系统环境变量 jdk增加环境变量JAVA_HOME=C:\Program Files\Java\jdk1.8.0_191(安装路径) 增加路径Pa ...

  9. arch linux下网易云音乐运行没反应,只能使用root用户运行

    本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/netease_music_can_not_open 最近打开网易 ...

  10. 判断List<E>内是否有重复对象

    主要用到Java 8的Stream类 long distinctedSize = list.stream().distinct().count(); boolean hasRepeat = list. ...