当前位置:网站首页>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
边栏推荐
- Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮
- Opencv combines multiple pictures into video
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
- Recursion - outputs continuously increasing numbers
- 腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
- Distributed system services
- Basic SQL (VIII) data update operation practice
- Winsock programming interface experiment: implementation of ipconfig
- 使用栈来解决”迷你语法分析器“的问题
- Depth deterministic strategy gradient (ddpg)
猜你喜欢

BLDC double closed loop (speed PI + current PI) Simulink simulation model

【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持

ASP.NET和ASP.NETCore多环境配置对比

Distributed system services

Configuring Apache Web services for servers such as Tianyi cloud

HLS / chisel practice CORDIC high performance computing complex square root

全网讲的最细,软件测试度量,怎样优化软件测试成本提高效率---火爆

.NET点滴:说说Middleware构造中获取不到Scoped服务的问题

Huawei machine test question -- deformation of hj53 Yang Hui triangle

Service avalanche effect
随机推荐
Redis Cluster集群,主节点故障,主从切换后ip变化,客户端需要处理不
Cherno_ Game engine series tutorial (5): 101~
C# 11 的这个新特性,我愿称之最强!
.NET点滴:说说Middleware构造中获取不到Scoped服务的问题
MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator
Q-Learning & Sarsa
The difference between encodeuri and encodeuricomponent
Introduction to ACM [inclusion exclusion theorem]
C# WPF UI框架MahApps切换主题
C# 11 对 ref 和 struct 的改进
如果通过 C# 实现对象的深复制 ?
Binary tree
MYSQL_ From mastery to abandonment
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
Numpy stack function
Er and eer models
使用split来解决“最常见的单词”问题
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
Huawei machine test question -- deformation of hj53 Yang Hui triangle
腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!