Skip to content

Commit

Permalink
0905 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Sep 16, 2018
1 parent 93be712 commit 9817dfa
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 0905-Sort-Array-By-Parity/cpp-0905/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.5)
project(A)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main3.cpp)
add_executable(A ${SOURCE_FILES})
33 changes: 33 additions & 0 deletions 0905-Sort-Array-By-Parity/cpp-0905/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// Source : https://leetcode.com/problems/sort-array-by-parity/solution/
/// Author : liuyubobobo
/// Time : 2018-09-15

#include <iostream>
#include <vector>

using namespace std;


/// Two Pass
/// Time Complexity : O(n)
/// Space Complexity: O(n)
class Solution {
public:
vector<int> sortArrayByParity(vector<int>& A) {

vector<int> ret;
for(int a: A)
if(a % 2 == 0)
ret.push_back(a);
for(int a: A)
if(a % 2)
ret.push_back(a);
return ret;
}
};


int main() {

return 0;
}
35 changes: 35 additions & 0 deletions 0905-Sort-Array-By-Parity/cpp-0905/main2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// Source : https://leetcode.com/problems/sort-array-by-parity/solution/
/// Author : liuyubobobo
/// Time : 2018-09-15

#include <iostream>
#include <vector>

using namespace std;


/// Sorting by custom comparator
/// Time Complexity : O(nlogn)
/// Space Complexity: O(1)
class Solution {
public:
vector<int> sortArrayByParity(vector<int>& A) {

sort(A.begin(), A.end(), cmp);
return A;
}

private:
static bool cmp(int a, int b){
if(a % 2 != b % 2)
return a % 2 == 0;
else
return a < b;
}
};


int main() {

return 0;
}
50 changes: 50 additions & 0 deletions 0905-Sort-Array-By-Parity/cpp-0905/main3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/// Source : https://leetcode.com/problems/sort-array-by-parity/solution/
/// Author : liuyubobobo
/// Time : 2018-09-15

#include <iostream>
#include <vector>

using namespace std;


/// Rearrange in place
/// Time Complexity : O(n)
/// Space Complexity: O(1)
class Solution {
public:
vector<int> sortArrayByParity(vector<int>& A) {

for(int i = 0; i < A.size(); i ++)
if(A[i] % 2){
int j = nextEven(A, i + 1);
if(j < A.size())
swap(A[i], A[j]);
}
return A;
}

private:
int nextEven(const vector<int>& A, int start){

for(int i = start; i < A.size(); i ++)
if(A[i] % 2 == 0)
return i;
return A.size();
}
};


void print_vec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}

int main() {

vector<int> nums {3, 1, 2, 4};
print_vec(Solution().sortArrayByParity(nums));

return 0;
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,6 @@ email: [[email protected]](mailto:[email protected])
| 901 | [Online Stock Span](https://leetcode.com/problems/online-stock-span/description/) | [solution](https://leetcode.com/problems/online-stock-span/solution/) | [C++](0901-Online-Stock-Span/cpp-0901/) | | |
| 902 | [Numbers At Most N Given Digit Set](https://leetcode.com/problems/numbers-at-most-n-given-digit-set/description/) | [solution](https://leetcode.com/problems/numbers-at-most-n-given-digit-set/solution/) | [C++](0902-Numbers-At-Most-N-Given-Digit-Set/cpp-0902/) | | |
| 903 | [Valid Permutations for DI Sequence](https://leetcode.com/problems/valid-permutations-for-di-sequence/description/) | [solution](https://leetcode.com/problems/valid-permutations-for-di-sequence/solution/)<br/>[缺:O(n^2) DP] | [C++](0903-Valid-Permutations-for-DI-Sequence/cpp-0903/) | | |
| | | | | | |
| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/description/) | [solution](https://leetcode.com/problems/sort-array-by-parity/solution/) | [C++](0905-Sort-Array-By-Parity/cpp-0905/) | | |
| | | | | | |

0 comments on commit 9817dfa

Please sign in to comment.