본문 바로가기

Algorithm/Leet Code

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

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


 2가지로 풀었는데, 한개는 퍼포먼스가 조금 더 잘나오고, 한개는 메모리 사용량이 적다.


I solved twice, one has slightly better performance, another one has better memory usage.


 


LeetCode #905

Q.

 Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.


You may return any answer array that satisfies this condition.


 주어진 양의 정수 배열 A 에서, A 의 모든 요소들을 포함하는 배열을 리턴하는데, 홀수여야 한다.


조건을 만족하는 어떤 배열이든 반환해도 괜찮다.

 


e.g. 

Input: [3,1,2,4]


Output: [2,4,3,1]


The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.



Process

// Process 1

//1. Input integer vector 

//2. Iterate from begin to the end 

// 2.1. If it's odd integer -> add to the end of the answerVector 

// 2.2. If it's even integer -> add to the begin of the answerVector 

//3. Return answerVector


// Process 2

//1. Input integer vector

//2. Iterate from begin to the end

// 2.1. If it's even integer -> move it to the begin

//3. Return vector



//처리과정 1

//1. 벡터 입력받는다.

//2. 시작부터 끝까지 반복한다.

// 2.1. 홀수면 -> answerVector 끝에 넣는다.

// 2.2. 짝수면 -> answerVector 앞에 넣는다.

//3. answerVector 반환한다.


//처리과정 2

//1. 벡터 입력받는다.

//2. 시작부터 끝까지 반복한다.

// 2.1. 짝수면 -> 맨 앞으로 옮긴다.

//3. 벡터 반환한다.



Code.. lemme see example code!!!

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




#include <vector>

#include <iostream>


using namespace std;


class Solution {

public:

// Process

//1. Input integer vector

//2. Iterate from begin to the end

// 2.1. If it's odd integer -> add to the end of the answerVector

// 2.2. If it's even integer -> add to the begin of the answerVector

//3. Return answerVector


// solution 1 - better performance

vector<int> sortArrayByParity(vector<int>& A) {

vector<int> answerVector;

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

{

if (A[i] % 2 != 0)  // if it's odd number

{

answerVector.push_back(A[i]);

}

else                // if it's even number

{

answerVector.insert(answerVector.begin(), A[i]);

}

}

return answerVector;

}


// Process

//1. Input integer vector

//2. Iterate from begin to the end

// 2.1. If it's even integer -> move it to the begin

//3. Return vector


//solution 2 - better memory usage

vector<int> sortArrayByParity(vector<int>& A) {


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

{

if (A[i] % 2 == 0)  // if it's odd number

{

A.insert(A.begin(), A[i]);

A.erase(A.begin() + i + 1);

}

}

return A;

}

};


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

std::vector<int> inputVector;

inputVector.push_back(3);

inputVector.push_back(1);

inputVector.push_back(2);

inputVector.push_back(4);


Solution sln;

vector<int> outputVector = sln.sortArrayByParity(inputVector);


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

{

cout << outputVector[i] << endl;

}

}





Something else you might like...?



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)



2018/12/26 - [Programming/Java] - JavaFx, how to show actual size image bigger than pane using scroll, 실제 크기 이미지를 스크롤바 사용하여 보여주는 방법, Java/cpp/kotlin/gui


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