当前位置:网站首页>利用正反遍历来解决“字符的最短距离”问题
利用正反遍历来解决“字符的最短距离”问题
2022-04-23 03:02:00 【&小小白&】
十七、字符的最短距离
17.1、题设要求
给你一个字符串 s 和一个字符 c ,且 c 是 s 中出现过的字符。
返回一个整数数组 answer ,其中 answer.length == s.length 且 answer[i] 是 s 中从下标 i 到离它 最近 的字符 c 的 距离 。
两个下标 i 和 j 之间的 距离 为 abs(i - j) ,其中 abs 是绝对值函数。
示例 1:
输入:s = "loveleetcode", c = "e"
输出:[3,2,1,0,1,0,0,1,2,2,1,0]
解释:字符 'e' 出现在下标 3、5、6 和 11 处(下标从 0 开始计数)。
距下标 0 最近的 'e' 出现在下标 3 ,所以距离为 abs(0 - 3) = 3 。
距下标 1 最近的 'e' 出现在下标 3 ,所以距离为 abs(1 - 3) = 2 。
对于下标 4 ,出现在下标 3 和下标 5 处的 'e' 都离它最近,但距离是一样的 abs(4 - 3) == abs(4 - 5) = 1 。
距下标 8 最近的 'e' 出现在下标 6 ,所以距离为 abs(8 - 6) = 2 。
示例 2:
输入:s = "aaab", c = "b"
输出:[3,2,1,0]
提示:
1 <= s.length <= 104
s[i] 和 c 均为小写英文字母
题目数据保证 c 在 s 中至少出现一次
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shortest-distance-to-a-character
17.2、解题思路
我们需要将字符串正向遍历一遍得出结果,然后再反向遍历一遍得出另一种结果,再将两种结果进行比较,将answer小的值放到一起即可。
17.3、算法
class Solution {
public int[] shortestToChar(String s, char c) {
int len = s.length();
int [] answer = new int[len];
//我们需要将prev赋值成两种情况,一种是特别大的值,一种是特别小的值
int prev = Integer.MIN_VALUE/2;
//正向遍历
for (int i = 0; i < len; i++) {
if (s.charAt(i) == c){
prev = i;
}
answer[i] = i - prev;
}
prev = Integer.MAX_VALUE/2;
//反向遍历
for (int i = len-1; i >= 0; i--) {
if (s.charAt(i) == c){
prev = i;
}
answer[i] = Math.min(answer[i],prev-i);
}
return answer;
}
}
参考视频:B站up主跑马拉松的程序员
版权声明
本文为[&小小白&]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_52916408/article/details/124304523
边栏推荐
- Niuke white moon race 6 [solution]
- Processes and threads
- Winsock programming interface experiment: implementation of ipconfig
- The input of El input input box is invalid, and error in data(): "referenceerror: El is not defined“
- HLS / chisel practice CORDIC high performance computing complex square root
- Shell script learning -- practical case
- [ncnn] - the meaning of - 23300 in param
- Binary tree
- AOT和单文件发布对程序性能的影响
- ASP.NET 6 中间件系列 - 执行顺序
猜你喜欢

Encapsulate components such as pull-down menu based on ele

Small companies don't make formal offers

Binary tree

Liunx foundation - zabbix5 0 monitoring system installation and deployment

AOT和单文件发布对程序性能的影响

Deep q-network (dqn)

微软是如何解决 PC 端程序多开问题的——内部实现

树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
![How to use C language to realize [guessing numbers game]](/img/8c/052dcb0ce64ee1713bebb1340248e6.png)
How to use C language to realize [guessing numbers game]
![[software testing] understand the basic knowledge of software testing](/img/ff/8fcd4b88de28505989aaf517d16113.png)
[software testing] understand the basic knowledge of software testing
随机推荐
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)
Cherno_ Game engine series tutorial (5): 101~
The shell monitors the depth of the IBM MQ queue and scans it three times in 10s. When the depth value exceeds 5 for more than two times, the queue name and depth value are output.
Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s
Wepy learning record
The space between the left and right of the movie ticket seats is empty and cannot be selected
The input of El input input box is invalid, and error in data(): "referenceerror: El is not defined“
LNMP MySQL allows remote access
Plug in for vscode
C# WPF UI框架MahApps切换主题
MYSQL_ From mastery to abandonment
Linux redis - redis database caching service
Chapter V project quality management of information system project manager summary
Navicat premium import SQL file
[ncnn] - the meaning of - 23300 in param
《信息系统项目管理师总结》第六章 项目人力资源管理
Q-Learning & Sarsa
Depth deterministic strategy gradient (ddpg)
Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
Detailed explanation of distributed things