821. Shortest Distance to a Character - LeetCode
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的更多相关文章
- 【Leetcode_easy】821. Shortest Distance to a Character
problem 821. Shortest Distance to a Character solution1: class Solution { public: vector<int> ...
- 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...
- LeetCode 821 Shortest Distance to a Character 解题报告
题目要求 Given a string S and a character C, return an array of integers representing the shortest dista ...
- [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 ...
- [Solution] 821. Shortest Distance to a Character
Difficulty: Easy Problem Given a string S and a character C, return an array of integers representin ...
- 821. Shortest Distance to a Character
class Solution { public: vector<int> shortestToChar(string S, char C) { int len=S.length(); ve ...
- [LeetCode] Shortest Distance to a Character 到字符的最短距离
Given a string S and a character C, return an array of integers representing the shortest distance f ...
- [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 ...
- [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 ...
随机推荐
- 单总线协议DS1820
一. DS18B20简介 DS18B20数字温度传感器接线方便,封装后可应用于多种场合,如管道式,螺纹式,磁铁吸附式,不锈钢封装式.主要根据应用场合的不同而改变其外观.封装后的DS18B20可用于电缆 ...
- 小程序输入框闪烁BUG解决方案
前言 本人所说的小程序,都是基于mpvue框架而上的,因此BUG可能是原生小程序的,也有可能是mpvue的. 问题描述 在小程序input组件中,如果使用v-model进行双向绑定,在输入时会出现光标 ...
- 单例模式的实现Singleton和MonoSingleton
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> / ...
- 【Android开发】URL 转义与反转义
1,转义 @org.junit.Test public void testEncode(){ String url="http://192.168.0.19:8888/cas/login&q ...
- 判断链表是否有环(Java实现)
判断给定的链表中是否有环.如果有环则返回true,否则返回false. 解题思路:设置两个指针,slow和fast,fast每次走两步,slow每次走一步,如果有环的话fast一定会追上slow,判断 ...
- MySQL外键约束On Delete和On Update的使用
On Delete和On Update都有Restrict,No Action, Cascade,Set Null属性.现在分别对他们的属性含义做个解释. ON DELETE restrict(约束) ...
- Django中间件、csrf跨站请求、csrf装饰器、基于django中间件学习编程思想
django中间件 中间件介绍 什么是中间件? 官方的说法:中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个轻量.低级别的插件系统,用于在全局范围内改变Django的输入和输出. ...
- springboot读取配置文件赋值给静态变量
1.实现InitializingBean接口,重写afterPropertiesSet方法,将@Value赋值给成员变量的属性赋值给静态变量,示例如下: /** * @Classname FileUt ...
- 函数 装饰器 python
今日内容概要 1.闭包函数 2.闭包函数的实际应用 3.装饰器简介(重点加难点) 4.简易版本装饰器 5.进阶版本装饰器 6.完整版本装饰器 7.装饰器模板(拷贝使用即可) 8.装饰器语法糖 9.装饰 ...
- 用js实现倒计时效果
首先获得两个时间的时间戳 var newdate = new Date('2021-01-22 21:25:00').getTime(); var olddate = new Date().getTi ...