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

  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

  

原文地址:https://www.cnblogs.com/chiyeung/p/9753492.html

时间: 2024-08-30 11:47:53

[LeetCode&Python] Problem 821. Shortest Distance to a Character的相关文章

[Solution] 821. Shortest Distance to a Character

Difficulty: Easy Problem 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,

[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 values of any two different nodes in the tree. Example : Input: root = [4,2,6,1,3,null,null] Output: 1 Explanation: Note that root is a TreeNode objec

821. Shortest Distance to a Character

1 class Solution 2 { 3 public: 4 vector<int> shortestToChar(string S, char C) 5 { 6 int len=S.length(); 7 vector<int>res(len,-1); //as the result vector 8 vector<int> cindex; //save the index of C 9 for(int i=0;i<len;i++) //write the

[LeetCode&amp;Python] Problem 905: Sort Array By Parity

Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A. You may return any answer array that satisfies this condition. Example 1: Input: [3,1,2,4] Output: [2,4,3,1] T

[LeetCode&amp;Python] Problem 806. Number of Lines To Write String

We are to write the letters of a given string S, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, it is written on the next line. We are given an arra

[LeetCode&amp;Python] Problem 811. Subdomain Visit Count

A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". When we visit

[LeetCode&amp;Python] Problem 682. Baseball Game

You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 following types: Integer (one round's score): Directly represents the number of points you get in this round. "+" (one round's score): Represents

[LeetCode&amp;Python] Problem 520. Detect Capital

Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to be right when one of the following cases holds: All letters in this word are capitals, like "USA". All letters in t

[LeetCode&amp;Python] Problem 427. Construct Quad Tree

We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or false. The root node represents the whole grid. For each node, it will be subdivided into four children nodes until the values in the region it repres