본문 바로가기

Algorithm/Leet Code

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:

  1. 2 <= A.length <= 20000
  2. A.length % 2 == 0
  3. 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...?

 

 

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/12/19 - [Algorithm/Leet Code] - LeetCode #706 DesignHashMap. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 데이터베이..

2019/12/11 - [Algorithm/Leet Code] - LeetCode #1047 RemoveAllAdjacentDuplicatesInString. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술..

2019/12/06 - [Algorithm/Leet Code] - LeetCode #844 BackspaceStringCompare. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 데이..

 

 

 

2019/11/02 - [Algorithm/Code Fights (Code Signal)] - CodeSignal Intro Databases #7 MostExpensive. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, ..

2019/10/31 - [Algorithm/Code Fights (Code Signal)] - CodeSignal Intro Databases #6 VolleyballResults. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면..

 

 

2019/08/14 - [Life/Item review] - Mi Band 4 review, 미밴드4 후기, 장점, 단점, 리뷰, 한글, global review, 미밴드4 글로벌 후기, 리뷰, 구매, 사용방법, setting, 세팅, ProsNCons

 

 

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/03/31 - [Life/Health care] - 동시에 먹지 말아야 하는 영양제, Health supplements which should not be taken together, 비타민, vitamin, usage/side effects/dosage 효능/부작용/성인,소아 용법

2019/02/10 - [Life/Health care] - 고지방식/키토식/키토제닉/hflc 다이어트 식단, What is the Keto diet/High fat low carb/hflc/ketogenic diet, 효과/효능/부작용/방법/effect/side effect

2019/01/17 - [Life/Health care] - Zinc 아연 usage/side effects/dosage/fatigue/supplement/antioxidant/효능/부작용/성인,소아 용법/건강/피로회복/영양제/항산화

2019/02/16 - [Life/Health care] - Finasteride 피나스테라이드,탈모약 usage/side effects/dosage 효능/부작용/효과/sexual effect/두타스테라이드/프로페시아/propecia/finpecia/카피약/copy drug/hair loss