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

要完成的函数:

vector<int> shortestToChar(string S, char C)

说明:

1、给定一个字符串S和字符C,找到字符串S中字符C的位置(可能有多个字符C),返回字符串S中所有字符距离最近的字符C的距离。

比如S为leetcode,C为e,那么返回的距离vector就是[3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]。

2、这道题题意很清晰,也有点像之前做过的一道题,笔者在这里模仿那道题的做法。

给一个例子说明一下,比如S为leetcodell,C为e。

那么我们先把每个字符都跟它右边或者本身的e比较距离,如果右边没有e了,那么插入INT_MAX。

遍历一遍字符串S,我们可以得到[1,0,0,4,3,2,1,0,INT_MAX,INT_MAX]。

接着我们再把每个字符都跟它左边或者本身的e比较距离,这个距离跟上述得到的距离,取一个最小值。如果左边没有e了,那么不做处理。

遍历一遍字符串S,最后得到的距离vector就是我们要的了。

上述做法,其实是把每个左边有e右边也有e的字符,计算一下距离左边的距离,再计算一下距离右边的距离,然后取一个最小值。

对于那些只有右边有e的字符,只处理一次,对于只有左边有e的字符,也只处理一次。

代码实现如下(附详解),分享给大家:

    vector<int> shortestToChar(string S, char C)
    {
        vector<int>index;//记录S中C的位置
        vector<int>res;//最后要返回的距离vector
        int s1=S.size();
        for(int i=0;i<s1;i++)//不断插入C的位置
        {
            if(S[i]==C)
                index.push_back(i);
        }
        int i=0,j=0,s2=index.size();
        while(i<s1)//计算每个字符跟右边C的距离
        {
            if(j<s2)//如果右边有C
            {
                if(i<index[j])
                {
                    res.push_back(index[j]-i);
                    i++;
                }
                else if((i==index[j]))
                {
                    res.push_back(0);
                    j++;
                    i++;
                }
            }
            else//如果右边没有C,那么插入INT_MAX
            {
                res.push_back(INT_MAX);
                i++;
            }
        }
        i=s1-1,j=s2-1;
        while(j>=0)//如果左边有C,计算每个字符跟左边C的距离
        {
            if(i>index[j])
            {
                res[i]=min(res[i],i-index[j]);//只保留最小值
                i--;
            }
            else if((i==index[j]))
            {
                j--;
                i--;
            }
        }
        return res;
    }

上述代码实测15ms,因为服务器接受到的cpp submissions有限,所以没有打败的百分比。

原文地址:https://www.cnblogs.com/king-3/p/9060001.html

时间: 2024-11-08 19:16:43

leetcode-821-Shortest Distance to a Character的相关文章

[LeetCode&amp;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 leng

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

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] 613. Shortest Distance in a Line_Easy tag: SQL

Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Write a query to find the shortest distance between two points in these points. | x | |-----| | -1 | | 0 | | 2 | The shortest distance is '1' obviously, w

(Easy) Shortest distance to Character LeetCode

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

[LeetCode] 244. Shortest Word Distance II 最短单词距离 II

This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it? Design a class which receives a list

[LeetCode] 243. Shortest Word Distance 最短单词距离

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. For example,Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. G

【leetcode】Edit Distance 详解

下图为TI C6xx DSP Nyquist总线拓扑图,总线连接了master与slave,提供了高速的数据传输.有很多种速率不同的总线,如图中的红色方框,最高速总线为CPU/2 TeraNet SCR(即VBUSM SCR),带宽为256bit,其他低速总线为CPU/3,CPU/6,带宽参考图中所示.总线之间用Bridge(桥)连接,作用包括转换总线的速率,使之与所流向总线的速率相同等. 在具体应用中,各种速率的总线完全可以满足复杂的数据传输,而数据传输的瓶颈往往在于连接总线之间的Bridge

pat 1046 Shortest Distance(20 分) (线段树)

1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input file contains one test case. For