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/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/26 - [Life/Health care] - Selenium 셀레늄 usage/side effects/dosage 효능/부작용/성인,소아 용법