본문 바로가기

Algorithm/Leet Code

LeetCode #821 ShortestDistanceToACharacter. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접..

LeetCode #821 ShortestDistanceToACharacter. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트

 

 

Runtime: 8 ms, faster than 97.03% of C++ online submissions for Shortest Distance to a Character.

 

Memory Usage: 8.9 MB, less than 100.00% of C++ online submissions for Shortest Distance to a Character.

 

 

LeetCode #821

Q.

 Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string.

 

 주어진 문자열 S 와 문자 C 가 있는데, 문자열의 문자들이 문자 C 까지의 가장 짧은 거리값을 나타내는 정수 배열을 반환해라.

 

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.

 

Process

// Process
//1. Input string S and char C
//2. Find char C indexes and make cIndexVector
//3. Iterate S (i)
// 3.1. Iterate cIndedVector (j)
//  3.1.1. Get shortestDistance from C, of S[i]
//  3.1.2. Put shortDistance to the resultVector
//4. Return resultVector

 

 

// 처리과정

//1. 문자열 S, 문자 C 입력받는다.

//2. 문자열 S 에서의 문자 C 가 있는 인덱스를 구해둔다. (cIndexVector)

//3. 문자열 S 반복한다. (i)

// 3.1. cIndexVector 반복한다. (j)

//  3.1.1. S[i] 에서 문자 C 까지 가장 짧은 거리를 구한다.

//  3.1.2. 가장 짧은 거리를 resultVector 에 넣는다.

//4. resultVector 반환한다.

 

 

Code.. lemme see example code!!!

코드.. 예제코드를 보자!!!

 

 

 

class Solution {
public:
	vector<int> shortestToChar(string S, char C) {
		vector<int> resultVector;
		vector<int> cIndexVector;
        // 2.
		for (int i = 0; i < S.length(); ++i) {
			if (S[i] == C) {
				cIndexVector.push_back(i);
			}
		}
        // 3.
		for (int i = 0; i < S.length(); ++i) {
			int shortDistance = 10000;
			for (int j = 0; j < cIndexVector.size(); ++j) {
                int distance = abs(i - cIndexVector[j]);
				if (distance < shortDistance) {
					shortDistance = distance;
				}
			}
			resultVector.push_back(shortDistance);
		}
		return resultVector;
	}
};

 

 

Something else you might like...?

 

 

2019/08/17 - [Algorithm/Leet Code] - LeetCode #1021 RemoveOutermostParentheses. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,..

2019/08/11 - [Algorithm/Leet Code] - LeetCode #896 MonotonicArray. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트

2019/08/08 - [Algorithm/Leet Code] - LeetCode #0448 FindAllNumbersDisappearedInAnArray. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기..

2019/08/06 - [Algorithm/Leet Code] - LeetCode #268 MissingNumber. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트

2019/08/05 - [Algorithm/Leet Code] - LeetCode #260 SingleNumber3. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트

2019/08/01 - [Algorithm/Leet Code] - LeetCode #872 LeafSimilarTree. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스..

2019/06/25 - [Algorithm/Leet Code] - LeetCode #766 ToeplitzMatrix. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트

2019/06/19 - [Algorithm/Leet Code] - LeetCode #771 JewelsAndStones. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스..

 

 

2018/10/16 - [Programming/Java] - Java enum class 자바 이넘 클래스 (Singleton design pattern 처럼 사용방법)

2018/10/02 - [Programming/Image Processing] - OpenCV Java/C++ FourierFastTransferShift(fftshift) 푸리에 변환 후 위치 재설정 함수 (Matlab - fftshift) - (1)

 

 

2018/12/28 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 - (#9, Tell, Don't Ask)

2018/12/26 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 (1)

 

2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #60 sudoku. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #59 spiralNumbers. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/08 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #58 messageFromBinaryCode. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/07 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #57 fileNaming. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/04 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #56 digitsProduct. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar

2019/01/04 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #55 differentSquares. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar

 

 

2018/12/28 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 - (#9, Tell, Don't Ask)

2018/12/26 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 (1)

 

 

2019/01/14 - [Programming/Java] - 자바 메모리 누수 체크/확인/고치는 방법, Memory leak check/fix in Java application, cleanCode/좋은코드/oop/객체지향

 

 

2019/02/19 - [Life/Health care] - Lysine 라이신 usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법

2019/02/16 - [Life/Health care] - Finasteride 피나스테라이드,탈모약 usage/side effects/dosage 효능/부작용/효과/sexual effect/두타스테라이드/프로페시아/propecia/finpecia/카피약/copy drug/hair loss

2019/02/25 - [Life/Health care] - Folic Acid 엽산 vitaminB9,비타민M usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법

2019/02/28 - [Life/Health care] - Vitamin K, 비타민 K usage/side effects/dosage 효능/부작용/성인,소아 용법

2019/03/03 - [Life/Health care] - Vitamin B1, Thiamine, 비타민 B1, 티아민 usage/side effects/dosage 효능/부작용/성인,소아 용법