From 9817dfa53041bba93e3301e56481f4fe031006f5 Mon Sep 17 00:00:00 2001 From: liuyubobobo Date: Sat, 15 Sep 2018 22:13:39 -0700 Subject: [PATCH] 0905 solved. --- .../cpp-0905/CMakeLists.txt | 7 +++ 0905-Sort-Array-By-Parity/cpp-0905/main.cpp | 33 ++++++++++++ 0905-Sort-Array-By-Parity/cpp-0905/main2.cpp | 35 +++++++++++++ 0905-Sort-Array-By-Parity/cpp-0905/main3.cpp | 50 +++++++++++++++++++ readme.md | 2 + 5 files changed, 127 insertions(+) create mode 100644 0905-Sort-Array-By-Parity/cpp-0905/CMakeLists.txt create mode 100644 0905-Sort-Array-By-Parity/cpp-0905/main.cpp create mode 100644 0905-Sort-Array-By-Parity/cpp-0905/main2.cpp create mode 100644 0905-Sort-Array-By-Parity/cpp-0905/main3.cpp diff --git a/0905-Sort-Array-By-Parity/cpp-0905/CMakeLists.txt b/0905-Sort-Array-By-Parity/cpp-0905/CMakeLists.txt new file mode 100644 index 00000000..5cc31b93 --- /dev/null +++ b/0905-Sort-Array-By-Parity/cpp-0905/CMakeLists.txt @@ -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}) \ No newline at end of file diff --git a/0905-Sort-Array-By-Parity/cpp-0905/main.cpp b/0905-Sort-Array-By-Parity/cpp-0905/main.cpp new file mode 100644 index 00000000..467acca3 --- /dev/null +++ b/0905-Sort-Array-By-Parity/cpp-0905/main.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/sort-array-by-parity/solution/ +/// Author : liuyubobobo +/// Time : 2018-09-15 + +#include +#include + +using namespace std; + + +/// Two Pass +/// Time Complexity : O(n) +/// Space Complexity: O(n) +class Solution { +public: + vector sortArrayByParity(vector& A) { + + vector 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; +} \ No newline at end of file diff --git a/0905-Sort-Array-By-Parity/cpp-0905/main2.cpp b/0905-Sort-Array-By-Parity/cpp-0905/main2.cpp new file mode 100644 index 00000000..fae8f8fd --- /dev/null +++ b/0905-Sort-Array-By-Parity/cpp-0905/main2.cpp @@ -0,0 +1,35 @@ +/// Source : https://leetcode.com/problems/sort-array-by-parity/solution/ +/// Author : liuyubobobo +/// Time : 2018-09-15 + +#include +#include + +using namespace std; + + +/// Sorting by custom comparator +/// Time Complexity : O(nlogn) +/// Space Complexity: O(1) +class Solution { +public: + vector sortArrayByParity(vector& 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; +} \ No newline at end of file diff --git a/0905-Sort-Array-By-Parity/cpp-0905/main3.cpp b/0905-Sort-Array-By-Parity/cpp-0905/main3.cpp new file mode 100644 index 00000000..be475978 --- /dev/null +++ b/0905-Sort-Array-By-Parity/cpp-0905/main3.cpp @@ -0,0 +1,50 @@ +/// Source : https://leetcode.com/problems/sort-array-by-parity/solution/ +/// Author : liuyubobobo +/// Time : 2018-09-15 + +#include +#include + +using namespace std; + + +/// Rearrange in place +/// Time Complexity : O(n) +/// Space Complexity: O(1) +class Solution { +public: + vector sortArrayByParity(vector& 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& 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& vec){ + for(int e: vec) + cout << e << " "; + cout << endl; +} + +int main() { + + vector nums {3, 1, 2, 4}; + print_vec(Solution().sortArrayByParity(nums)); + + return 0; +} \ No newline at end of file diff --git a/readme.md b/readme.md index a5a375ab..c58ec8dd 100644 --- a/readme.md +++ b/readme.md @@ -513,4 +513,6 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 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/)
[缺: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/) | | | | | | | | | | \ No newline at end of file