当前位置:网站首页># Leetcode 821. 字符的最短距离(简单)
# Leetcode 821. 字符的最短距离(简单)
2022-06-27 17:57:00 【我是胖虎啊】
题目名称
821. 字符的最短距离
自己想的解法
题目思路
- 遍历一遍字符串s,获取记录预期字符c在s中所有位置的列表 list_c
- 定义一个方法: 获取输入字符 和 列表中所有元素 所有差值中绝对值最小的那个值
- 遍历字符串s,每遍历到一个字符时,调用一次自定义方法,记录到数组中
code for Python3
class Solution:
def shortestToChar(self, s: str, c: str) -> List[int]:
list_c = [i for i in range(len(s)) if s[i] == c]
arr = []
for j in range(len(s)):
arr.append(self.get_abs_minnum(j, list_c))
return arr
def get_abs_minnum(self, first_num:int, li:List):
cur = pow(10, 4)
for v in li:
cur = min(cur, abs(v - first_num))
return cur
复杂度分析
- 时间复杂度: O(N²) 原因: 双层循环, 每次遍历字符串s时,都会遍历一次list_c
- 空间复杂度: O(N) 原因: list_c占用的空间大小和字符串s的长度有关
官方题解
仔细研究了一下官方题解, 发现思路特别的巧妙, 其思路值得借鉴!
题目思路
- 先从左到右遍历一次S, 记录当前字符与C距离的绝对值.在未出现预期值前,该位置用正无穷替代;出现预期值后,记录实际距离
- 从右往左遍历一次S,同样的 记录当前字符与C距离的绝对值.
- 在第2次遍历过程中, 取当前遍历结果的绝对值 与 第1次遍历值 的最小值,添加到数组中
code for Python3
class Solution(object):
def shortestToChar(self, S, C):
prev = float('-inf')
arr = []
for i, x in enumerate(S):
if S[i] == C:
prev = i
else:
arr.append(i - prev)
prev = float('inf')
for i in range(len(S) -1, -1, -1):
if S[i] == C:
prev = i
else:
arr.append(min(prev-i, arr[i]))
return arr
复杂度分析
- 时间复杂度: O(N) 原因: 仅遍历了2次字符串S
- 空间复杂度: O(N) 原因: arr数组的长度
python的相关知识
- enumerate 方法: 在输出数据结构的索引 和 值的时候使用
s = "abcdefg"
for i, j in enumerate(s):
print(i, j)
结果为:
0 a
1 b
2 c
3 d
4 e
5 f
6 g
--------------------------------------------------
for k in enumerate(s):
print(k)
结果为:
(0, 'a')
(1, 'b')
(2, 'c')
(3, 'd')
(4, 'e')
(5, 'f')
(6, 'g')
- 关于python中的无穷大, 无穷小
定义无穷大: float('inf')
定义无穷小: float('-inf')
特殊情况:
0*无穷大 or 0*无穷小 结果为nan(不是一个数, 所有和nan的相关计算都无法获取结果)
其它的示例:
float('nan') + 9999999 # nan
float('nan') - 9999999 # nan
float('nan') * 9999999 # nan
0 - 无穷小 -> 无穷大
0 - float('-inf') # float('inf')
- 列表的倒序输出
s = [1,2,3,4,5]
for i in range(len(s) - 1, -1, -1):
print(s[i])
结果为:
5
4
3
2
1边栏推荐
- redis集群系列三
- A simple calculation method of vanishing point
- 一对一关系
- 【登录界面】
- Is it safe to buy stocks online and open an account?
- One to one relationship
- 通过 G1 GC Log 重新认识 G1 垃圾回收器
- The IPO of Yuchen Airlines was terminated: Guozheng was proposed to raise 500million yuan as the major shareholder
- 1030 Travel Plan
- Cdga | what is the core of digital transformation in the transportation industry?
猜你喜欢

binder hwbinder vndbinder

从感知机到前馈神经网络的数学推导

【登录界面】

Oracle 获取月初、月末时间,获取上一月月初、月末时间

GIS remote sensing R language learning see here

过关斩将,擒“指针”(下)

基于STM32F103ZET6库函数按键输入实验

Blink SQL built in functions

Function key input experiment based on stm32f103zet6 Library

The Fifth Discipline: the art and practice of learning organization
随机推荐
What is ssr/ssg/isr? How do I host them on AWS?
Making single test so simple -- initial experience of Spock framework
Substrate及波卡一周技术更新速递 20220425 - 20220501
Where to look at high-yield bank financial products?
基于STM32F103ZET6库函数外部中断实验
Is it safe to buy stocks and open an account on the account opening link of the securities manager? Ask the great God for help
数组练习 后续补充
VS code 运行yarn run dev 报yarn : 无法加载文件XXX的问题
谈谈线程安全
如何利用 RPA 实现自动化获客?
网络传输是怎么工作的 -- 详解 OSI 模型
DCC888 :Register Allocation
如何封装调用一个库
图扑数字孪生智慧能源一体化管控平台
Jinyuan's high-end IPO was terminated: it was planned to raise 750million Rushan assets and Liyang industrial investment were shareholders
redis集群系列二
The Fifth Discipline: the art and practice of learning organization
OpenSSL client programming: SSL session failure caused by an obscure function
运算符的基础知识
华大单片机KEIL报错_WEAK的解决方案