본문 바로가기

Algorithm/Leet Code

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

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




 Spent almost 30 min, problem was easy so thought about performance while solving but not as good as I want and solved three times.


First one, make iteration each 15, 3, 5 multiple number

Second one, make iteration and insert if statement using % operator

Third one, make iteration and insert if statement, but didn't use % operator


Performance..  3 >> 1 = 2




 30분정도 걸림, 문제 자체는 쉬워서 성능 생각하면서 풀었는데 별로 좋지 않아서 다시 풀고 또 다시 풀고 해서 3번 품


첫번째는 3,5,15 배수 각각 배수로 반복문해서 넣어줌

두번째는 반복문에 if 문 넣어서 해줌, % 오퍼래이터 사용

세번째는 반복문에 if 문 넣는데 조건에 % 기호 없이 직접 해줌


퍼포먼스는 3 >>  1 = 2 였다.



LeetCode #412

Q.

 Write a program that outputs the string representation of numbers from 1 to n.


But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.



 1부터 n 까지의 수를 나타내는 스트링을 출력하는 프로그램을 만들어라


하지만 3의 배수는 "Fizz" 로 나타내고, 5의 배수는 "Buzz" 로 나타낸다. 3과 5 공배수는 "FizzBuzz" 로 나타낸다.


 


e.g. 

 Example:

n = 15,

Return:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]




Process

// Process

//1. Input integer n

//2. Iterate from 1 to n

// 2.1. Check if it's multiple of three, five or both

//  2.1.1. If so -> Add proper string

//  2.1.2. If not -> Add int string

//3. Return answerVector


// 처리과정

//1. 정수 n 을 입력받는다.

//2. 1부터 n 까지 반복한다.

// 2.1. 3, 5 또는 둘다의 배수인지 확인해서

//  2.1.1. 맞으면 -> 해당 글자를 넣는다.

//  2.1.2. 아니면 -> 정수를 넣는다.

//3. 반환한다.





Code.. lemme see example code!!!

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





#include <vector>

#include <string>

#include <iostream>

#include <sstream>



using namespace std;



class Solution {

public:

// 1st

vector<string> fizzBuzz(int n) {

vector<string> answerVector;

int fizz = 1;

int buzz = 1;

for (int i = 1; i <= n; ++i)

{

if (fizz == 3 && buzz == 5)

{

answerVector.push_back("FizzBuzz");

fizz = 0;

buzz = 0;

}

else if (fizz == 3)

{

answerVector.push_back("Fizz");

fizz = 0;

}

else if (buzz == 5)

{

answerVector.push_back("Buzz");

buzz = 0;

}

else

{

answerVector.push_back(to_string(i));

}

++fizz;

++buzz;

}

return answerVector;

}


// 2nd

//vector<string> fizzBuzz(int n) {


// vector<string> answerVector;


// for (int i = 1; i <= n; ++i)

// {

// if (i % 15 == 0)

// {

// answerVector.push_back("FizzBuzz");

// }

// else if (i % 3 == 0)

// {

// answerVector.push_back("Fizz");

// }

// else if (i % 5 == 0)

// {

// answerVector.push_back("Buzz");

// }

// else 

// {

// answerVector.push_back(to_string(i));

// }

// }

// return answerVector;

//}


// 3rd

//vector<string> fizzBuzz(int n) {


// vector<string> answerVector;


// for (int i = 1; i <= n; ++i)

// {

// answerVector.push_back(to_string(i));

// }


// for (int i = 3; i <= n; i+=3)

// {

// answerVector.at(i - 1) = "Fizz";

// }


// for (int i = 5; i <= n; i+=5)

// {

// answerVector.at(i - 1) = "Buzz";

// }


// for (int i = 15; i <= n; i += 15)

// {

// answerVector.at(i - 1) = "FizzBuzz";

// }


// return answerVector;

//}

};



int main(int argc, char *argv[]) {


Solution sln;

vector<string> returnVector = sln.fizzBuzz(25);


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

{

cout << returnVector.at(i) << endl;

}


}





Something else you might like...?



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

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

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

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

2019/02/12 - [Algorithm/Leet Code] - LeetCode #136 SingleNumber. 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/16 - [Algorithm/Leet Code] - LeetCode #217 ContainsDuplicate. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

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

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

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



2018/10/19 - [Programming/Design Pattern ] - Design pattern - Prototype (디자인패턴 - 프로토타입) / Java C++ C#



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