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. JS 字符串 作为变量名

    function initCKEditor(querySelector,content_val,myEditor) { ClassicEditor.create(document.querySelec ...

  2. python通过get方式,post方式发送http请求和接收http响应-urllib urllib2

    python通过get方式,post方式发送http请求和接收http响应-- import urllib模块,urllib2模块, httplib模块 http://blog.163.com/xyc ...

  3. spring-cloud: eureka之:ribbon负载均衡自定义配置(二)

    spring-cloud: eureka之:ribbon负载均衡自定义配置(二) 有默认配置的话基本上就是轮询接口,现在我们改用自定义配置,同时支持:轮询,随机接口读取 准备工作: 1.eureka服 ...

  4. 基因家族收缩和扩张分析 & Selective loss pathway & 泛基因组

    套路 这通常就是基因组组装后的必做分析,通过比较基因组学的手段进行分析,可以知道所研究物种在进化过程中哪些核心基因家族发生了变化,从而导致了其特殊的适应性机制的形成. 参考: Extremotoler ...

  5. UVA-12558 Egyptian Fractions (HARD version) (IDA* 或 迭代加深搜索)

    题目大意:经典的埃及分数问题. 代码如下: # include<iostream> # include<cstdio> # include<cstring> # i ...

  6. inherit

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace { cl ...

  7. EBS管理员为供应商创建新联系人流程

    管理员为供应商创建新联系人流程 /oracle/apps/pos/supplier/webui/ByrAddCntctPG oracle.apps.pos.supplier.webui.ByrAddC ...

  8. Executors类的newFixedThreadPool, newCachedThreadPool, newScheduledThreadPool

    Executors 类对 ThreadPoolExecutor 的构造函数进行了封装,使用该类可方便地创建线程池. 1. newFixedThreadPool public static Execut ...

  9. 给Ajax一个漂亮的嫁衣——Ajax系列之五(下)之序列化和反序列化

    给Ajax一个漂亮的嫁衣——Ajax系列之五(下)之序列化和反序列化 标签: ajaxdictionaryjsonobject服务器function 2012-07-25 18:41 2242人阅读  ...

  10. react中为什么要使用immutable

    因为在react中,react的生命周期中的setState()之后的shouldComponentUpdate()阶段默认返回true,所以会造成本组件和子组件的多余的render,重新生成virt ...