当前位置:网站首页>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]
- JS relearning
- 7-11 重排链表 (25 分)
- tf. keras. layers. Timedistributed function
- .NET点滴:说说Middleware构造中获取不到Scoped服务的问题
- 微软是如何解决 PC 端程序多开问题的
- 荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
- The difference between encodeuri and encodeuricomponent
- C#中元组对象Tuple的使用
- Introduction and use of openfeign component
猜你喜欢

ASP.NET和ASP.NETCore多环境配置对比
![[software testing] understand the basic knowledge of software testing](/img/ff/8fcd4b88de28505989aaf517d16113.png)
[software testing] understand the basic knowledge of software testing
![Niuke white moon race 5 [problem solving mathematics field]](/img/be/ca059bd1c84eaaaefa3266f9119a6b.png)
Niuke white moon race 5 [problem solving mathematics field]

TP5 customization in extend directory succeeded and failed. Return information

HLS / chisel practice CORDIC high performance computing complex square root

Openfeign timeout setting

tf. keras. layers. Conv? D function

基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?

Summary of interface automation interview questions for software testing

Response processing of openfeign
随机推荐
Distributed system services
Opencv reads webcam video and saves it locally
Openfeign timeout setting
Judge whether there is a leap year in the given year
MYSQL05_ Ordr by sorting, limit grouping, group by grouping
What kind of experience is it to prepare for a month to participate in ACM?
《信息系統項目管理師總結》第六章 項目人力資源管理
C# 11 的这个新特性,我愿称之最强!
Laravel new route file
REINFORCE
微软是如何解决 PC 端程序多开问题的——内部实现
使用DFS来解决“字典序排数”问题
Creating wechat voucher process with PHP
Basic SQL (VIII) data update operation practice
tf. keras. layers. Density function
7-11 重排链表 (25 分)
Binary tree
L2-006 树的遍历(中后序确定二叉树&层序遍历)
How to count the number of all files in a directory under win10 system
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system