LeetCode #922 SortArrayByParity2. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 데이터..
LeetCode #922 SortArrayByParity2. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 데이터베이스, sql, query, 쿼리
Runtime: 88 ms, faster than 49.29% of C++ online submissions for Sort Array By Parity II.
Memory Usage: 14.3 MB, less than 12.50% of C++ online submissions for Sort Array By Parity II.
LeetCode #922
Q.
Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.
Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.
You may return any answer array that satisfies this condition.
음수가 아닌 정수들의 배열인 A 가 주어지는데, 절반은 홀수이고 절반은 짝수이다. 정렬을 하는데, 홀수번째 값은 홀수를 넣고, 짝수번째 값은 짝수를 넣는다. 이 두개만 만족하면 어떤 답이든 정답이다.
Example 1:
Input: [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
Note:
- 2 <= A.length <= 20000
- A.length % 2 == 0
- 0 <= A[i] <= 1000
Process
// Process
//1. Input integers vector
//2. Iterate from begin to the end
// 2.1. Check if it's odd or even
// 2.1.1. If it's odd -> put to odd vector
// 2.1.2. If it's even -> put to even vector
//3. Make resultVector (even / odd / even / odd ...)
//4. Return resultVector
// 처리과정
//1. 정수 배열 A 를 입력받는다.
//2. 시작부터 끝까지 반복한다.
// 2.1. 홀수면 홀수배열에 넣는다.
// 2.2. 짝수면 짝수배열에 넣는다.
//3. 홀짝배열의 길이 끝까지 반복한다. (동일함)
// 3.1. 리턴배열에 짝수를 넣는다.
// 3.2. 리턴배열에 홀수를 넣는다.
//4. 리턴배열 반환한다.
Code.. lemme see example code!!!
코드.. 예제코드를 보자!!!
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& A) {
vector<int> resultVector;
vector<int> oddVector;
vector<int> evenVector;
for (int i = 0; i < A.size(); ++i)
{
if (A[i] % 2 == 1)
{
oddVector.push_back(A[i]);
}
else
{
evenVector.push_back(A[i]);
}
}
for (int i = 0; i < oddVector.size(); ++i)
{
resultVector.push_back(evenVector[i]);
resultVector.push_back(oddVector[i]);
}
return resultVector;
}
};
Something else you might like...?