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代码
单总线协议DS1820代码 一.DS18B20初始化 (1).数据线拉到低电平"0". (2).延时480微妙(该时间的时间范围可以从480到960微妙). (3).数据线拉到高电 ...
- PCB中的生产工艺、USB布线、特殊部件、蓝牙天线设计
PCB中的生产工艺.USB布线.特殊部件.蓝牙天线设计 (2016-07-20 11:43:27) 转载▼ PCB生产中Mark点设计 1.pcb必须在板长边对角线上有一对应整板定位的Mark ...
- 首次写iPad布局感想(H5)
一直做前端工作,却从来没有开发过平板的项目,想来也是有遗憾的,孰知,新公司的第二个项目就是要适配平板,刚开始是懵的,对于兼容,感觉是自己的短板,但庆幸的是这一版只需要兼容iOS系统就可以. 那我现在就 ...
- Hadoop 3.1.2报错:xception in thread "main" org.apache.hadoop.fs.UnsupportedFileSystemException: No FileSystem for scheme "hdfs"
报错内容如下: Exception in thread "main" org.apache.hadoop.fs.UnsupportedFileSystemException: No ...
- springAop必导jar包
SpringAop:的底层就是通过JDK动态代理"或"CGLib动态代理为技术目标织入横切逻辑. 做aop:需要导入: spring-aop-4.1.5.RELEASE.jar s ...
- 彻底理解synchronized
1. synchronized简介 在学习知识前,我们先来看一个现象: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public ...
- 微信小程序获取当前时间戳、获取当前时间、时间戳加减
//获取当前时间戳 var timestamp = Date.parse(new Date()); timestamp = timestamp / 1000; console.log("当前 ...
- 通过面试题学JavaScript知识(1)
// a 是多少的时候 可以让下面的打印ok if(a == 1 && a == 2 && a ==3){ console.log('ok') } 分析1: == 比较 ...
- 文档声明(Doctype)和<!Doctype html>有何作用? 严格模式与混杂模式如何区分?它们有何意义?
文档声明的作用: 文档声明是为了告诉浏览器,当前HTML文档使用什么版本的HTML来写的,这样浏览器才能按照声明的版本来正确的解析. <!doctype html> 的作用就是让浏览器进入 ...
- python---选择排序的实现
选择排序 思想 一趟遍历记录最小的数, 放到第一个位置 再一趟遍历记录剩余列表中最小的数, 继续放置 关键点: 无序区: 第i趟, 无序区为 i~n-1 最小数的位置 import r ...