forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93be712
commit 9817dfa
Showing
5 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/) | | | | ||
| | | | | | | |