当前位置:网站首页>The shortest distance of dynamic programming characters
The shortest distance of dynamic programming characters
2022-04-22 12:53:00 【Frank. Ren】
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 .

class Solution {
public int[] shortestToChar(String s, char c) {
int len = s.length();
int[] ret = new int[len];
Arrays.fill(ret, len);
for (int i = 0, j = -1; i < len; i++) {
if (s.charAt(i) == c) {
j = i;
}
if (j != -1) {
ret[i] = i - j;
}
}
for (int i = len - 1, j = -1; i >= 0; i--) {
if (s.charAt(i) == c) {
j = i;
}
if (j != -1) {
ret[i] = Math.min(ret[i], j - i);
}
}
return ret;
}
}

版权声明
本文为[Frank. Ren]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221251148296.html
边栏推荐
- JS基础6
- 给自己成功的可能性
- Ros2 learning notes (10) learn the workspace of ros2 from turnlesim
- Secondary development of ABAQUS RSG plug-in (II)
- Mathematics - covariance
- 数学——协方差
- Page d'inscription du projet Bookstore et vérification de la boîte aux lettres
- 【JZ48 最长不含重复字符的子字符串】
- The R language uses the treemap function in the treemap package to visualize the treemap graph: treemap displays the hierarchical data as a group of nested rectangles, customized label color, label ba
- 多线程基础一 线程的创建
猜你喜欢

制裁压力下,俄罗斯计划通过友好国家平行进口电子产品

Detailed explanation and code example of serial communication of 51 single chip microcomputer

.NET宝藏API之:OutputFormatter,格式化输出对象

3D应用旋转相册

ABAQUS py command flow of cantilever beam under parabolic pressure

Baidu map combines VR panorama, and panorama map mode makes you feel more at ease when traveling

TypeError: connection.connect is not a function

. net treasure API: outputformatter, format output object

分享一下自己最近写的一个移动端项目积累的一些实用技巧

51单片机案例(1)——利用DS1302实现实时时钟和可调时钟的功能
随机推荐
R language ggplot2 visualization: ggplot2 visualization moves the legend of the image to the inside of the image and customizes the location of the legend
MySQL 5.0 installation tutorial illustration detailed tutorial
TypeError: connection.connect is not a function
Ros2 learning notes (10) learn the workspace of ros2 from turnlesim
396. Rotation function
智慧文旅逐渐数字化,vr全景推动文旅一体化发展
51单片机案例(1)——利用DS1302实现实时时钟和可调时钟的功能
VNC连接Too many security failures
诺瓦星云更新招股书,继续上市进程,“学术派”的步步为盈?
微服务的简单部署
The R language uses the treemap function in the treemap package to visualize the treemap graph: treemap displays the hierarchical data as a group of nested rectangles, customized label color, label ba
TypeError: connection. connect is not a function
R language splits dataframe into two or more dataframe actual combat (splitting tables): Using R native method, data Table, dplyr and other schemes
April 21, 2022 s s s m framework integration @ lesson 1 s s m environment configuration & focus on practical operation. Summary in practice. The results are not shown here.
JS基础6
分享一下自己最近写项目遇到的小问题
Use of sqlserver cursor
分享一下自己最近写的一个移动端项目积累的一些实用技巧
挑选了适合测试边界的汉字及截图
書城項目注册頁面和郵箱驗證