字符的最短距离
2025-4-20
| 2025-4-20
Words 448Read Time 2 min
type
status
date
slug
summary
tags
category
icon
password

题目:

给你一个字符串 s 和一个字符 c ,且 cs 中出现过的字符。
返回一个整数数组 answer ,其中 answer.length == s.lengthanswer[i]s 中从下标 i 到离它 最近 的字符 c距离
两个下标 ij 之间的 距离abs(i - j) ,其中 abs 是绝对值函数。
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/SNJvJP 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
示例 1:
示例 2:
提示:
  • 1 <= s.length <= 104
  • s[i]c 均为小写英文字母
  • 题目数据保证 cs 中至少出现一次

思路:

  • 遍历一次,求出所有与c相同的字符,装进list里
  • 再次双重遍历,求出距离最短的

代码:

结果:

notion image
image-20220410153333260
存在重复元素字符串中第二大的数字
Loading...