当前位置:网站首页>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
边栏推荐
- Summary of software test interview questions
- tf. keras. layers. MaxPooling? D function
- It turns out that PID was born in the struggle between Lao wangtou and Lao sky
- tf. keras. layers. Embedding function
- Guangcheng cloud service can fill in a daily report regularly every day
- 如果通过 C# 实现对象的深复制 ?
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
- Chapter VI project information management system summary
- 荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
- Judge whether there is a leap year in the given year
猜你喜欢
Configuring Apache Web services for servers such as Tianyi cloud
ASP.NET 6 中间件系列 - 执行顺序
Guangcheng cloud service can fill in a daily report regularly every day
Onenet connection process
Processes and threads
Laravel8- use JWT
ASP.NET 6 中间件系列 - 条件中间件
Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
HLS / chisel practice CORDIC high performance computing complex square root
Niuke white moon race 6 [solution]
随机推荐
Load view Caton
Plug in for vscode
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
Cherno_ Game engine series tutorial (5): 101~
Simple example of using redis in PHP
How to deploy a website with only a server and no domain name?
使用两种方法来解决“最大回文数乘积”问题
L2-006 树的遍历(中后序确定二叉树&层序遍历)
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
使用栈来解决”迷你语法分析器“的问题
Onenet connection process
HLS / chisel uses CORDIC hyperbolic system to realize square root calculation
先中二叉建树
Small companies don't make formal offers
微软是如何解决 PC 端程序多开问题的
Creating wechat voucher process with PHP
Introduction to ACM [inclusion exclusion theorem]
《信息系統項目管理師總結》第六章 項目人力資源管理
全网讲的最细,软件测试度量,怎样优化软件测试成本提高效率---火爆