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. Streamlit:快速数据可视化界面工具

    目录 Streamlit简介 Streamlit使用指南 常用命令 显示文本 显示数据 显示图表 显示媒体 交互组件 侧边栏 缓存机制 Streamlit使用Hack Streamlit的替代品 相关 ...

  2. 体验javascript之美6:如果你觉得什么都会了或者不知道js学什么了看这里-面向对象编程

    概述 当大家已经把js的语言基础理解了,然后能够写出一些简单的例子了,这个时候基本上达到了一年工作经验的水平,而自己能够独立的写一些小功能,完成一些小效果,或者临摹修改一些比较复杂的插件的时候差不多就 ...

  3. C++:Abstract class : invalid abstract return type for member function ‘virtual...’

    #include <iostream> #include <cmath> #include <sstream> using namespace std; class ...

  4. ubantu系统之安装notepadqq

    Ubuntu下的安装方法:     sudo add-apt-repository ppa:notepadqq-team/notepadqq     sudo apt-get update     s ...

  5. java中final变量的用法

    4.4 final变量    final变量的数值不能在初始化之后进行改变(你希望a=3,有很多用到a的场合, 你当然不能在程序中就用3来代替a). 比如: final int h = 0; 想像有一 ...

  6. lunix或者centos服务器下如何下载自己在github上面的项目代码

    1.在github找到项目压缩包下载地址 打开自己的github主页找到需要下载的项目首页,如图所示,找到zip下载地址(ps:如何找这个地址我就不多说了,了解过一点html的同学肯定很容易可以找到) ...

  7. FastAPI(七十二)实战开发《在线课程学习系统》接口开发-- 留言列表开发

    之前我们分享了FastAPI(七十一)实战开发<在线课程学习系统>接口开发-- 查看留言,这次我们分享留言列表开发. 列表获取,也需要登录,根据登录用户来获取对应的留言.逻辑梳理如下. 1 ...

  8. EbitenCookBook中文教程 第一课:安装 Ebiten

    本文实时更新原址:https://ebitencookbook.vercel.app/docs/CookBook_Start/class1 第一课 安装 Ebiten 欢迎大家来到 Ebiten 中文 ...

  9. JDK1.8.0_181的无限制强度加密策略文件变动(转载)

    JDK1.8.0_181的无限制强度加密策略文件变动 原文地址 https://my.oschina.net/my1313677/blog/3109613 作者 葉者 日常记录 2019/09/23 ...

  10. asp.net core启动源码以及监听,到处理请求响应的过程

    摘要 asp.net core发布至今已经将近6年了,很多人对于这一块还是有些陌生,或者说没接触过:接触过的,对于asp.net core整个启动过程,监听过程,以及请求过程,响应过程也是一知半解,可 ...