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

  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.

Accepted

42.5K

Submissions

66.4K

Seen this question in a real interview before?

Solution:

class Solution {
    public int[] shortestToChar(String S, char C) {

        ArrayList <Integer> arr = new ArrayList();
        int[] res = new int[S.length()];

        for(int i = 0; i<S.length(); i++){

            if(S.charAt(i) == C){

                arr.add(i);
            }
        }

        for(int i = 0; i<S.length(); i++){
            int tmp = Integer.MAX_VALUE;
            for(int j = 0; j<arr.size();j++ ){

                if(Math.abs(arr.get(j)-i)<tmp){

                    tmp = Math.abs(arr.get(j)-i);
                }
            }

            res[i] =tmp;
        }

        return res;

    }
}

原文地址:https://www.cnblogs.com/codingyangmao/p/11395919.html

时间: 2024-08-30 15:06:45

(Easy) Shortest distance to Character LeetCode的相关文章

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

[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

A1046——入门模拟 Shortest Distance

2019-12-15 15:25:34 #include <bits/stdc++.h> #include<math.h> using namespace std; const int MAXN = 100005; int main(){ int N; cin>>N; int temp[N+1]; for(int i=0;i<N+1;++i){ temp[i] = 0; } for(int i = 1;i<N+1;++i){ cin>>temp[

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