본문 바로가기

Algorithm/Leet Code

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

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



 금방 떠오르고 금방 코딩됐지만 퍼포먼스가 꽝


Solution came up with very easily and coding easily too, but performance is shitty




LeetCode #557

Q.

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.


 주어진 문자열에서, 각각의 단어 안의 문자들이 뒤집혀 있어야 한다. 각 문자는 스페이스로 떨어져있다.



e.g. 

Example 1:


Input: "Let's take LeetCode contest"

Output: "s'teL ekat edoCteeL tsetnoc"


Note: In the string, each word is separated by single space and there will not be any extra space in the string.

노트 : 문자열에, 각 문자는 한개의 스페이스바로 분리되어있고, 다른 추가 스페이스바는 없다.


Process

// Process

//1. Input sentence s

//2. Iterate till the end

// 2.1. Make word chunk separated by white space

// 2.2. Reverse word

// 2.3. Put reversed word to resultString

//3. Return resultString


// 처리과정

//1. 문자열을 입력받는다.

//2. 끝까지 반복한다.

// 2.1. 스페이스까지 문자 덩어리를 만든다.

// 2.2. 만든 문자덩어리를 뒤집는다.

// 2.3. 뒤집은 문자를 결과문자열에 넣는다.

//3. 결과문자열 반환한다.




Code.. lemme see example code!!!

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





class Solution {

private:

string reverseWord(string wordChunk) {

string reversedWord = "";

for (int i = wordChunk.size()-1; i >= 0; --i)

{

reversedWord += wordChunk[i];

}

return reversedWord;

}

public:

string reverseWords(string s) {

string resultString = "";

string wordChunk = "";

for (int i = 0; i < s.size(); ++i)

{

if (s[i] != ' ')

{

wordChunk += s[i];

}

else

{

resultString += reverseWord(wordChunk);

resultString += ' ';

wordChunk = "";

}

}

resultString += reverseWord(wordChunk);


return resultString;

}

};





Something else you might like...?





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

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

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

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

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

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





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/01/25 - [Life/Health care] - L-Arginine 아르기닌 usage/side effects/dosage 효능/부작용/성인,소아 용법(3)

2018/12/16 - [Life/Health care] - L-Arginine 아르기닌 usage/side effects/dosage 효능/부작용/성인,소아 용법(2)

2018/12/27 - [Life/Health care] - Milk-Thistle 밀크시슬 usage/side effects/dosage/fatigue/supplement,효능/부작용/성인,소아 용법/건강/피로회복/영양제

2018/12/26 - [Life/Health care] - Selenium 셀레늄 usage/side effects/dosage 효능/부작용/성인,소아 용법