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. django关系类型字段

    一.多对一(ForeignKey) 多对一的关系,通常被称为外键.外键字段类的定义如下: class ForeignKey(to, on_delete, **options)[source] 外键需要 ...

  2. C#中的约束类型

  3. API网关 动态路由、监控、授权、安全、调度

    1.API网关介绍 API网关是一个服务器,是系统的唯一入口.从面向对象设计的角度看,它与外观模式类似.API网关封装了系统内部架构,为每个客户端提供一个定制的API.它可能还具有其它职责,如身份验证 ...

  4. Confluence 6 LDAP 用户组结构设置

    用户组对象类(Group Object Class) 这是在 LDAP 用户组对象中使用的类的名字.例如: groupOfUniqueNames group 用户组对象过滤器(Group Object ...

  5. hdu2328 kmp

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  6. Python并行(parallel)之谈

    简介 可以先看看并发Concurrent与并行Parallel的区别 在谈并行前,头脑中总会浮出多线程.多进程.线程/进程同步.线程/进程通信等词语. 那为什么需要同步.通信,它们之间的作用是怎样的呢 ...

  7. hdu-6319-单调队列

    Problem A. Ascending Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K ...

  8. Oracle11g温习-第八章:归档日志

    2013年4月27日 星期六 10:36 1.归档和非归档的区别 1)  归档会在日志切换时,备份历史日志,用于OLTP(在线事务处理系统),可以进行冷备份和热备份,可以实现数据库完全恢复.不完全恢复 ...

  9. Oracle 11g dataguard check RTA(real time apply)

    Oracle 11g dataguard check RTA(real time apply) 2017年8月24日 16:38 环境:oracle 11.2.0.1 OEL 5.8 注:以下操作都在 ...

  10. Leetcode 73

    class Solution { public: void setZeroes(vector<vector<int>>& matrix) { vector<vec ...