[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:
S
string length is in[1, 10000].
C
is a single character, and guaranteed to be in stringS
.- All letters in
S
andC
are 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 ...
随机推荐
- 03_ExeZZ.cpp
1.静态加载 DLL : #pragma comment(lib, "DllZZ.lib") __declspec(dllimport) void __stdcall AA(); ...
- VC6的VCVARS32.BAT所在位置
1. C:\Program Files (x86)\Microsoft Visual Studio\VC98\Bin\VCVARS32.BAT 2.
- [osg]osg自定义事件的理解
参考:http://blog.csdn.net/l_andy/article/details/51058907 添加自定义事件 首先osg在其内部通过osgGA::EventQueue类维护了一个事件 ...
- js 文件上传
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...
- ASCII 和 Unicode 编码的由来
大话数据结构上的说明: 网络博文的说明:
- C#中正确使用enum做Key的姿势
C#中自定义enum,然后将其作为Dictionary的Key,通常的做法如下: using System; using System.Text; using System.Collections.G ...
- Windows 2008 更改网卡绑定顺序
用 ncpa.cpl 或者用鼠标右键点网上邻居进去也好. 来到网卡列表画面. 然后,你会发觉没有菜单去操作[高级设置], 这里,最高级的步骤来了, 就是你需要按一个 [Alt]把菜单给显示出来,太神奇 ...
- WPF自定义控件的两种方式
方法A: 第一步:My自定义控件:Control 第二步:针对 “My自定义控件” 类型,编写<style>或<模板>(UI的外观完全由用户自己定义) 第三步: 使用My自定 ...
- CentOS 7 Install Gitlab CE
https://hostpresto.com/community/tutorials/how-to-install-and-setup-gitlab-on-centos-7/ http://linux ...
- vs2012修改代码编辑区域的背景色