Question

821. Shortest Distance to a Character

Solution

思路:遍历字符串S,遇到与字符C相等就分别向左/右计算其他字符与该字符的距离,如果其他字符就是C或与目标字符的距离更小的话遍历就终止。

Java实现:

public int[] shortestToChar(String S, char C) {
int[] store = new int[S.length()];
for (int i = 0; i < S.length(); i++) {
if (S.charAt(i) == C) {
store[i] = 0;
left(S, C, store, i);
right(S, C, store, i);
}
}
return store;
} void left(String S, char C, int[] store, int target) {
for (int i = target - 1; i >= 0; i--) {
if (S.charAt(i) == C) break;
if (store[i] == 0 || store[i] > target - i) store[i] = target - i;
}
} void right(String S, char C, int[] store, int target) {
for (int i = target + 1; i < S.length(); i++) {
if (S.charAt(i) == C) break;
if (store[i] == 0 || store[i] > i - target) store[i] = i - target;
}
}

821. Shortest Distance to a Character - LeetCode的更多相关文章

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

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

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

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

  3. LeetCode 821 Shortest Distance to a Character 解题报告

    题目要求 Given a string S and a character C, return an array of integers representing the shortest dista ...

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

  5. [Solution] 821. Shortest Distance to a Character

    Difficulty: Easy Problem Given a string S and a character C, return an array of integers representin ...

  6. 821. Shortest Distance to a Character

    class Solution { public: vector<int> shortestToChar(string S, char C) { int len=S.length(); ve ...

  7. [LeetCode] 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] 821. Shortest Distance to a Character_Easy tag: BFS

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

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

随机推荐

  1. 安装ESLint

    安装ESLint ESLint是静态代码检查工具,配合TypeScript使用可以帮助检查TypeScript的语法和代码风格. 添加ESLint到当前工程,yarn add -D eslint. 使 ...

  2. CEPH-4:ceph RadowGW对象存储功能详解

    ceph RadosGW对象存储使用详解 一个完整的ceph集群,可以提供块存储.文件系统和对象存储. 本节主要介绍对象存储RadosGw功能如何灵活的使用,集群背景: $ ceph -s clust ...

  3. CSS3渐变效果工具

    推荐一个css3渐变效果工具,觉得有帮助的可以收藏下. 工具链接 CSS3 渐变(gradients)可以让你在两个或多个指定的颜色之间显示平稳的过渡.CSS3 定义了两种类型的渐变(gradient ...

  4. java中Error和Exception用法上有什么区别,Error是怎么回事?

    顺便提一句, 和Exception 相对应的,还有Error,Error(错误)表示系统级的错误和程序不必处理的异常,是JRE(java运行环境)的内部错误或者硬件问题,比如,另外 某一处地方的bug ...

  5. 虚拟机上 安装 CentoOS 7.5 1804 过程记录

    1.准备安装镜像 在开始安装CentOS之前,必须下载安装ISO映像.镜像可从CentOS网站https://www.centos.org/download/.提供以下基本类型的镜像: DVD ISO ...

  6. 利用Docker快速部署Mysql

    写在前面 我又来更新了~~~,今天内容较少,主要是利用Docker快速部署Mysql和初始化数据 利用Docker下载Mysql 简洁明了,在命令提示符中输入 docker pull mysql:8. ...

  7. 利用 onnxruntime 库同时推理多个模型的效率研究

    1. 背景 需求:针对视频形式的数据输入,对每一帧图像,有多个神经网络模型需要进行推理并获得预测结果.如何让整个推理过程更加高效,尝试了几种不同的方案. 硬件:单显卡主机. 2. 方案 由于存在多个模 ...

  8. hyperledger 儿童车级开发项目实战----投票系统(1)

    今天根据hyperledger 企业级开发项目实战视频,自己做了一个投票demo.在这做个记录 首先编写智能合约 在$GOPATH的的src路径下创建项目的名称,我的是mkdir vote 然后创建c ...

  9. GEOS 使用记录

    GEOS 使用记录 官网 https://trac.osgeo.org/geos/ https://libgeos.org/ 下载地址 https://libgeos.org/usage/downlo ...

  10. 一文搞懂MySQL事务的隔离性如何实现|MVCC

    关注公众号[程序员白泽],带你走进一个不一样的程序员/学生党 前言 MySQL有ACID四大特性,本文着重讲解MySQL不同事务之间的隔离性的概念,以及MySQL如何实现隔离性.下面先罗列一下MySQ ...