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:

  1. S string length is in [1, 10000].
  2. C is a single character, and guaranteed to be in string S.
  3. All letters in S and C 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的更多相关文章

  1. 【Leetcode_easy】821. Shortest Distance to a Character

    problem 821. Shortest Distance to a Character solution1: class Solution { public: vector<int> ...

  2. 821. Shortest Distance to a Character - LeetCode

    Question 821. Shortest Distance to a Character Solution 思路:遍历字符串S,遇到与字符C相等就分别向左/右计算其他字符与该字符的距离,如果其他字 ...

  3. 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...

  4. LeetCode 821 Shortest Distance to a Character 解题报告

    题目要求 Given a string S and a character C, return an array of integers representing the shortest dista ...

  5. [Solution] 821. Shortest Distance to a Character

    Difficulty: Easy Problem Given a string S and a character C, return an array of integers representin ...

  6. [LeetCode&Python] Problem 461. Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  7. [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 ...

  8. [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 ...

  9. 821. Shortest Distance to a Character

    class Solution { public: vector<int> shortestToChar(string S, char C) { int len=S.length(); ve ...

随机推荐

  1. Android TableLayout中的使用说明

    TableLayout特点: 1)TableLayout和我们平时在网页上见到的Table有所不同,TableLayout没有边框的 2)它是由多个TableRow对象组成,每个TableRow可以有 ...

  2. cocos2dx 在windows下开启console

    cocos2dx 3.10 1.在main函数中写入代码 AllocConsole(); freopen("CONIN$", "r", stdin); freo ...

  3. Codeforces 545D - Queue

    545D - Queue 思路:忍耐时间短的排在前面,从小到大排序,贪心模拟,记录当前等待时间,如过等待时间大于当前的这个人得忍耐时间,那么就把这个人扔到最后面,不要管他了(哼╭(╯^╰)╮,谁叫你那 ...

  4. English trip -- VC(情景课)5 Around Town

     Around Town  城市周围 Talk about the picture 看图说话 sentences Where are you? I'm in the Meten classroom. ...

  5. 20170706wdVBA正则表达式提取题目

    Public Sub GetContents() Dim Reg As Object Dim Matches As Object Dim OneMatch As Object Dim Index As ...

  6. 4-1 contag_tag:返回HTMLtag.

    jquery已经过时,做一遍,了解其他知识点. contag_tag(name, content_or_options_with_block = nil, options = nil, &bl ...

  7. Android之省市区三级联动

    最近项目要做一个电商APP,选择收货地址的三级联动滚动选择组件, 控件用起来非常简单 ,下面是它的运行效果: 布局 <LinearLayout xmlns:android="http: ...

  8. hdu 2266 dfs+1258

    How Many Equations Can You Find Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  9. OAF 供应商门户添加功能标签后获取当前供应商VendorId的方法

    一种是参考管理页面 /oracle/apps/pos/supplier/webui/SuppDtPG 在目标页面的AM中添加VO实例,oracle.apps.pos.supplier.server.S ...

  10. XMLItergration.java

    /*===========================================================================+ | Copyright (c) 2001, ...