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. css 学习笔记 菜鸟

    1 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明: 选择器通常是您需要改变样式的 HTML 元素. 每条声明由一个属性和一个值组成.每个属性有一个值.属性和值被冒号分开.CSS声明总是 ...

  2. 31、[源码]-AOP原理-AnnotationAwareAspectJAutoProxyCreato机

    31.[源码]-AOP原理-AnnotationAwareAspectJAutoProxyCreato机

  3. 第一章使用JSP/Server技术开发新闻发布系统第一章动态网页开发基础

      一:为什么需要动态网页    由于静态网页的内容是固定的,不能提供个性化和定制化得服务,使用动态网页可真正地与用户实现互动. 二:什么是动态网页  ①:动态网页是指在服务器端运行的,使用程序语言设 ...

  4. scheduled定时任务+实例请求数据库

    1.scheduled定时任务类:ScheduledDemo.java package com.nantian.scheduled; import java.util.Date; import org ...

  5. [Luogu] 树链剖分

    模板题,对于对为某个点为根的子树进行处理时,只需每个节点记录两个值 分别为搜索以该节点为根的子树时的最初搜索序和最末搜索序,将这两 个数作为线段树区间操作的端点进行操作 #include <bi ...

  6. python3 中的bytes类型

  7. ZROI Day6比赛总结

    比赛还没结束而且我没有参加比赛就来这里了. T1 略 T2 设\(ans_d\)表示\(d|b_i\)的方案数(最后反演一下就可以) 设\(d\not|a_i\)的个数为\(l\)(可以\(O(n\l ...

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

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

  9. NNDL练习——Numpy的简单使用

    总结自nndl_exercise Numpy导入 import numpy as np 数组/矩阵的创建 a=np.array([1,2,3]) b=np.array([[1,2],[3,4]]) c ...

  10. ELK系列(7) - 测试环境下Logstash异常退出:block in multi_receive_encoded

    问题与分析 在本地测试无误后将ELK部署到了测试环境,结果第二天发现Logstash挂掉了,一开始以为是自动部署之类导致的问题.之后几天时间里Logstash总是会down掉,查看了下日志文件,发现报 ...