Problem #1 Shortest Distance to a Character
Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string.
Example 1:
Input: S = "loveleetcode", C = "e"
Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]
Note:
S string length is in [1, 10000].
C is a single character, and guaranteed to be in string S.
All letters in S and C are lowercase.
</>复制代码
class Solution {
public int[] shortestToChar(String S, char C) {
int len = S.length();
int[] res = new int[len];
if (S == null || S.length() == 0) return res;
Arrays.fill(res, 10000);
int pre = -1;
for (int i = 0; i < len; i++) {
char ch = S.charAt(i);
if (ch == C) {
pre = i;
res[i] = 0;
} else {
if (pre != -1) {
res[i] = i-pre;
}
}
}
pre = -1;
for (int i = len-1; i >= 0; i--) {
char ch = S.charAt(i);
if (ch == C) {
pre = i;
} else {
if (pre != -1) {
res[i] = Math.min(res[i], pre-i);
}
}
}
return res;
}
}
Problem #2
Solution #2
Problem #3
Solution #3
Problem #4
Solution #4
Problem #5
Solution #5
Problem #6
Solution #6
Problem #7
Solution #7
Problem #8
Solution #8
Problem #9
Solution #9
Problem #10
Solution #10
Problem #11
Solution #11
Problem #12
Solution #12
Problem #13
Solution #13
Problem #14
Solution #14
Problem #15
Solution #15
Problem #16
Solution #16
Problem #17
Solution #17
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/76741.html
摘要:遇到的问题今天工作上遇到一个相关的问题,关于字符串格式化的。是根据计算机用户所使用的语言,所在国家或者地区,以及当地的文化传统所定义的一个软件运行时的语言环境。 locale遇到的问题 今天工作上遇到一个 locale 相关的问题,关于字符串格式化的。不过让我们先从 locale 说起。 locale 简介 什么是locale locale 这个单词中文翻译成地区或者地域,其实这...
摘要:简介参考指南参考指南中文全文搜索引擎顾名思义,是做收索用的,因为普通的搜索在数据量小的时候,能应对自如,但是当数据量上千万,或者上亿的时候。 sphinx简介参考指南参考指南sphinx API中文 sphinx 全文搜索引擎:顾名思义,是做收索用的,因为普通的搜索在数据量小的时候,能应对自如,但是当数据量上千万,或者上亿的时候。纯粹的sql搜索显得缓慢,无力。因而为了提高用户体验,增...
摘要:建立长度与目标串相等的模式函数初始化,为,之后,若不重复,赋,若有重复段,赋对应的模式函数值不难,建议死记硬背根据模式函数用两个指针比较两个字符串,当目标串指针和目标串长度相等时,返回差值。 Implement strStr() Problem Implement strStr(). Returns the index of the first occurrence of needle...
摘要:各一个指针,表示上一次真正到的位置。在的时候,上,增加下一步知道出界,发现是错误的所以需要回到上次的地方,即一旦走出去,无法返回,需要一个指针记录最后的地方。 public class Solution { public boolean isMatch(String s, String p) { int idxs = 0, idxp = 0, idxmatch ...
阅读 1766·2023-04-26 00:25
阅读 1098·2021-09-27 13:36
阅读 1004·2019-08-30 14:14
阅读 2256·2019-08-29 17:10
阅读 1091·2019-08-29 15:09
阅读 2038·2019-08-28 18:21
阅读 1051·2019-08-26 13:27
阅读 1062·2019-08-26 10:58