본문 바로가기

Algorithm/Leet Code

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

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

 

5min

 

 

Runtime: 40 ms, faster than 68.48% of C++ online submissions for DI String Match.

 

Memory Usage: 10.5 MB, less than 7.14% of C++ online submissions for DI String Match.

 

 

LeetCode #942

Q.

 Given a string S that only contains "I" (increase) or "D" (decrease), let N = S.length.

Return any permutation A of [0, 1, ..., N] such that for all i = 0, ..., N-1:

  • If S[i] == "I", then A[i] < A[i+1]
  • If S[i] == "D", then A[i] > A[i+1]

 string S 가 주어지는데, 'I' 아니면 'D' 로 이루어져 있다. S 의 length 는 N 이라한다.

0 ~ N 까지 숫자가 들어가는 어떤 순열을 반환해라

 

  • If S[i] == "I", then A[i] < A[i+1]     S[i] 번쨰 값이 'I' 이면 뒤의 수보다 작다.
  • If S[i] == "D", then A[i] > A[i+1]    S[i] 번째 값이 'D' 이면 앞의 수보다 크다.

 

 

Example 1:

 

Input: "IDID"

Output: [0,4,1,3,2]

 

 

Example 2:

 

Input: "III"

Output: [0,1,2,3]

 

 

Example 3:

 

Input: "DDI"

Output: [3,2,0,1]

 

 

Note:

  1. 1 <= S.length <= 10000
  2. S only contains characters "I" or "D".

 

Process

// Process
//1. Input string S
//2. Check start int I, D
//3. Iterate from begin to the end of S (i)
// 3.1. Check if it's I or D
//  3.1.1. If it's I -> push_back(intI) and ++
//  3.1.1. If it's D -> push_back(intD) and --
//4. Return resultVector

 

 

// 처리과정

//1. 스트링 S 를 입력받는다.

//2. 시작 int I 와 D 를 정해둔다.

//3. S 의 시작부터 끝까지 반복한다.

// 3.1. I 인지 D 인지 확인해서

//  3.1.1. I 이면 -> intI 를 리턴벡터에 넣고 ++ 한다.

//  3.1.2. D 이면 -> intD 를 리턴벡터에 넣고 -- 한다.

//4. Return resultVector

 

Code.. lemme see example code!!!

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

 

 

 

class Solution {
public:
    vector<int> diStringMatch(string S) {
        vector<int> resultVec;
        int intI = 0;
        int intD = S.length();
        
        for (int i = 0; i < S.length(); ++i) {
            if (S[i] == 'I') {
                resultVec.push_back(intI++);
            } else { // 'D'
                resultVec.push_back(intD--);
            }
        }
        resultVec.push_back(intI);
        return resultVec;
    }
};

 

 

 

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 효능/부작용/성인,소아 용법