Aracade Intro #55 differentSquares. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar
Q.
Given a rectangular matrix containing only digits, calculate the number of different 2 × 2 squares in it.
숫자만 들어있는 주어진 매트릭스에서, 각기 안에 다른 숫자를 가진 2x2 사이즈 사각형의 개수를 구하여라.
e.g.
Input -> matrix = [[1, 2, 1],
[2, 2, 2],
[2, 2, 2],
[1, 2, 3],
[2, 2, 1]]
Output -> differentSquares(matrix) = 6
Here are all 6 different 2 × 2 squares:
1 2
2 2
2 1
2 2
2 2
2 2
2 2
1 2
2 2
2 3
2 3
2 1
총 6개
//Process
//1. Input int matrix
//2. Iterate from begin to row - 1
// 2.1. Iterate from begin to column - 1
// 2.1.1. check if square number is in squareArray
// 2.1.1.1 If not -> add
//3. Return squareArray length
//처리과정
//1. matrix를 입력받는다.
//2. 시작부터 끝-1 까지 반복한다. (row)
// 2.1. 시작부터 끝-1 까지 반복한다. (column)
// 2.1.1. 사각형 숫자들이 이미 나왔었는지 확인해서
// 2.1.1.1. 없으면 -> 추가해둔다.
//3. 사각형숫자들 나온 총 개수를 출력한다.
Code.. Lemme see code!!!!!
코드.. 코드를 보자!!!!!
int differentSquares(std::vector<std::vector<int>> matrix) {
std::vector<string> squaresVector;
bool answer = false;
if (matrix.size() > 1 && matrix[0].size() > 1)
{
string tempBuffer;
for (int i = 0; i < matrix.size() - 1; ++i)
{
for (int j = 0; j < matrix[0].size() - 1; ++j)
{
tempBuffer = "";
tempBuffer = itos(matrix[i][j]) + itos(matrix[i][j+1]) +
itos(matrix[i+1][j]) + itos(matrix[i+1][j+1]);
// cout << tempBuffer << endl;
if (!vectorHasElement(&squaresVector, tempBuffer))
{
squaresVector.push_back(tempBuffer);
}
}
}
}
return squaresVector.size();
}
string itos(int integer) {
std::stringstream ss;
ss << integer;
return ss.str();
}
bool vectorHasElement(std::vector<string> *squaresVector, string element) {
bool answer = false;
for (int k = 0; k < squaresVector->size(); ++k)
{
if (element == squaresVector->at(k))
{
answer = true;
}
}
return answer;
}
Something else you might like..
2018/12/26 - [Programming/Design Pattern ] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍
2019/01/02 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #52 longestWord. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2019/01/01 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #51 deleteDigit. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/31 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #50 chessKnight. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/28 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #49 lineEncoding. Algorithm, 알고리즘 문제풀이, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/26 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #48 isDigit. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/26 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #47 isMAC48Address. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/25 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #46 electionsWinners. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/21 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #45 buildPalindrome. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/20 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #44 findEmailDomain. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/17 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #43 isBeautifulString. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/16 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #42 bishopAndPawn. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/14 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #41 digitDegree. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #40 longestDigitsPrefix. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/10 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #39 knapsackLight. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/20 - [Programming/Java] - How to convert file to byte array (byte[]) example, 파일 객체 바이트로 변환하기 예제코드, java/cpp/c++/scalar
2018/12/18 - [Programming/Java] - How to access controller from existing Pane in JavaFX, pane에서 해당 controller로 접근하기, java/cpp/c++/fx/gui
2018/12/13 - [Computer/Linux] - How to check ongoing live process on Linux cmd, 리눅스 커맨드창에서 어떤 프로세스가 실행중인지 확인하기, java, ubuntu, cpp
2018/12/27 - [Life/Health care] - Milk-Thistle 밀크시슬 usage/side effects/dosage/fatigue/supplement,효능/부작용/성인,소아 용법/건강/피로회복/영양제
2018/12/16 - [Life/Health care] - L-Arginine 아르기닌 usage/side effects/dosage 효능/부작용/성인,소아 용법(2)
2018/12/06 - [Life/Health care] - Vitamin C 비타민 씨 usage/side effects/dosage 용법/효능/부작용
2018/12/02 - [Life/Health care] - Maca 마카 usage/side effects/dosage 효능/부작용/용법