当前位置:网站首页>Using positive and negative traversal to solve the problem of "the shortest distance of characters"
Using positive and negative traversal to solve the problem of "the shortest distance of characters"
2022-04-23 03:05:00 【& Xiaobai &】
seventeen 、 The shortest distance of a character
17.1、 Question design requirements
Give you a string s And a character c , And c yes s Characters that have appeared in .
Returns an array of integers answer , among answer.length == s.length And answer[i] yes s From the subscript i To leave it lately The characters of c Of distance .
Two subscripts i and j Between distance by abs(i - j) , among abs Is an absolute value function .
Example 1:
Input :s = "loveleetcode", c = "e"
Output :[3,2,1,0,1,0,0,1,2,2,1,0]
explain : character 'e' Appears in the subscript 3、5、6 and 11 It's about ( Subscript from 0 Start counting ).
Distance subscript 0 Current 'e' Appears in the subscript 3 , So the distance is abs(0 - 3) = 3 .
Distance subscript 1 Current 'e' Appears in the subscript 3 , So the distance is abs(1 - 3) = 2 .
For subscripts 4 , Appears in the subscript 3 And subscripts 5 Situated 'e' Are closest to it , But the distance is the same abs(4 - 3) == abs(4 - 5) = 1 .
Distance subscript 8 Current 'e' Appears in the subscript 6 , So the distance is abs(8 - 6) = 2 .
Example 2:
Input :s = "aaab", c = "b"
Output :[3,2,1,0]
Tips :
1 <= s.length <= 104
s[i] and c All are lowercase English letters
Topic data assurance c stay s At least once in
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/shortest-distance-to-a-character
17.2、 Their thinking
We need to traverse the string forward to get the result , Then go through it in reverse and get another result , Then compare the two results , take answer Small values can be put together .
17.3、 Algorithm
class Solution {
public int[] shortestToChar(String s, char c) {
int len = s.length();
int [] answer = new int[len];
// We need to prev The assignment is divided into two cases , One is a particularly large value , One is a very small value
int prev = Integer.MIN_VALUE/2;
// Positive traversal
for (int i = 0; i < len; i++) {
if (s.charAt(i) == c){
prev = i;
}
answer[i] = i - prev;
}
prev = Integer.MAX_VALUE/2;
// Reverse traversal
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;
}
}
Reference video :B standing up The programmer who runs the Marathon
版权声明
本文为[& Xiaobai &]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301476638.html
边栏推荐
猜你喜欢
![Niuke white moon race 5 [problem solving mathematics field]](/img/be/ca059bd1c84eaaaefa3266f9119a6b.png)
Niuke white moon race 5 [problem solving mathematics field]
![[software testing] understand the basic knowledge of software testing](/img/ff/8fcd4b88de28505989aaf517d16113.png)
[software testing] understand the basic knowledge of software testing

Detailed explanation of distributed things

Laravel8- use JWT

腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!

tf. keras. layers. Embedding function

Opencv combines multiple pictures into video

Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮

利用栈的回溯来解决“文件的最长绝对路径”问题

Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
随机推荐
tf. keras. layers. Density function
Er and eer models
Openfeign service call
The space between the left and right of the movie ticket seats is empty and cannot be selected
C#中切片语法糖的使用
中后二叉建树
tf. keras. layers. Conv? D function
Openfeign timeout setting
Typescript Learning Guide
How to count the number of all files in a directory under win10 system
Opencv fills the rectangle with a transparent color
如果通过 C# 实现对象的深复制 ?
Regular object type conversion tool - Common DOM class
由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。
Numpy append function
L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
Detailed log display of openfeign call
利用栈的回溯来解决“文件的最长绝对路径”问题
Processes and threads