[LeetCode&Python] Problem 821. 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:
Sstring length is in[1, 10000].Cis a single character, and guaranteed to be in stringS.- All letters in
SandCare lowercase.
class Solution:
def shortestToChar(self, S, C):
"""
:type S: str
:type C: str
:rtype: List[int]
"""
S2=S[::-1]
indexOfC=S.index(C)
indexOfC2=S2.index(C)
ans1=[]
ans2=[]
n=len(S) for i in range(n):
if S[i]==C:
ans1.append(0)
indexOfC=i
else:
ans1.append(abs(i-indexOfC)) if S2[i]==C:
ans2.append(0)
indexOfC2=i
else:
ans2.append(abs(i-indexOfC2)) ans2=ans2[::-1] for i in range(n):
if ans1[i]>ans2[i]:
ans1[i]=ans2[i] return ans1
[LeetCode&Python] Problem 821. Shortest Distance to a Character的更多相关文章
- 【Leetcode_easy】821. Shortest Distance to a Character
problem 821. Shortest Distance to a Character solution1: class Solution { public: vector<int> ...
- 821. Shortest Distance to a Character - LeetCode
Question 821. Shortest Distance to a Character Solution 思路:遍历字符串S,遇到与字符C相等就分别向左/右计算其他字符与该字符的距离,如果其他字 ...
- 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...
- LeetCode 821 Shortest Distance to a Character 解题报告
题目要求 Given a string S and a character C, return an array of integers representing the shortest dista ...
- [Solution] 821. Shortest Distance to a Character
Difficulty: Easy Problem Given a string S and a character C, return an array of integers representin ...
- [LeetCode&Python] Problem 461. Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode&Python] Problem 783. Minimum Distance Between BST Nodes
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- [LeetCode&Python] Problem 748. Shortest Completing Word
Find the minimum length word from a given dictionary words, which has all the letters from the strin ...
- 821. Shortest Distance to a Character
class Solution { public: vector<int> shortestToChar(string S, char C) { int len=S.length(); ve ...
随机推荐
- python里的apply,applymap和map的区别
apply,applymap和map的应用总结: apply 用在dataframe上,用于对row或者column进行计算: applymap 用于dataframe上,是元素级别的操作: map ...
- angular5 ng-bootstrap和ngx-bootstrap区别
https://angular.cn/resources ngx-bootstrap 安装: npm install ngx-bootstrap --save 再引入css <link href ...
- Appium 坑
1. [Android]click没有反应 https://testerhome.com/topics/9610 在某些手机上有个安全选项,需要在开发者选项->安全设置(允许模拟点击),打开开关
- freemarker中对null值问题的处理
1. freemarker不支持null. 如果值为null会报错. 2.当值为null的处理 1)过滤不显示 Hello ${name!} 在属性后面加感叹号即可过滤null和空字符串 if和”?? ...
- Python处理HTML转义字符
抓网页数据经常遇到例如>或者 这种HTML转义符,抓到字符串里很是烦人. 比方说一个从网页中抓到的字符串: html = '<abc>' 用Python可以这样处理: import ...
- python下编译py成pyc和pyo和pyd
https://www.cnblogs.com/dkblog/archive/2009/04/16/1980757.html
- codeforces 484b//Maximum Value// Codeforces Round #276(Div. 1)
题意:给一个数组,求其中任取2个元素,大的模小的结果最大值. 一个数x,它的倍数-1(即kx-1),模x的值是最大的,然后kx-2,kx-3模x递减.那么lower_bound(kx)的前一个就是最优 ...
- 『Scrapy』爬虫框架入门
框架结构 引擎:处于中央位置协调工作的模块 spiders:生成需求url直接处理响应的单元 调度器:生成url队列(包括去重等) 下载器:直接和互联网打交道的单元 管道:持久化存储的单元 框架安装 ...
- SQL Server日志清空方法
1.清空日志:DUMP TRANSACTION databasename WITH NO_LOG 2.截断事务日志:BACKUP LOG databasename WITH ...
- bzoj3404
题解: 博弈论 然而我直接暴力dp 代码: #include<bits/stdc++.h> using namespace std; ; int f[N],n,T; void init() ...