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 distance of C and build the cindex
10             if(S[i]==C)
11             {
12                 cindex.push_back(i);
13                 res[i]=0;
14             }
15         int szindex=cindex.size();
16
17         int count=1;
18         for(int j=cindex[0]-1;j>=0;j--)           //write the distance before the first C
19             res[j]=count++;
20
21         count=1;
22         for(int k=cindex[szindex-1]+1;k<len;k++)  //write the distance after the last C
23             res[k]=count++;
24
25         for(int x=1;x<szindex;x++)                //write the distance between two C
26         {
27             count=1;
28             int left=cindex[x-1]+1,right=cindex[x]-1;
29             while(left<=right)
30             {
31                 res[left++]=count;
32                 res[right--]=count++;
33             }
34         }
35         return res;
36     }
37 };

原文地址:https://www.cnblogs.com/zhuangbijingdeboke/p/9189307.html

时间: 2024-08-30 17:26:49

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,

(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-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 strin

[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

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

PAT——甲级1046S:shortest Distance

这道题,折磨了我一个多小时,前前后后写了三个算法. 1046 Shortest Distance (20 point(s)) 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

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

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 each case, the first line con