sort with custom comp function
This commit is contained in:
@@ -144,6 +144,6 @@ bool earlier_date (const Date& a, const Date& b) {
|
||||
|
||||
## 3.11 Exercises
|
||||
|
||||
- [Leetcode problem 905: Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/). Solution: [p905_sortarraybyparity.cpp](../../leetcode/p905_sortarraybyparity.cpp)
|
||||
- [Leetcode problem 977: Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/). Solution: [p977_sortedsquare.cpp](../../leetcode/p977_sortedsquare.cpp)
|
||||
- [Leetcode problem 1051: Height Checker](https://leetcode.com/problems/height-checker/). Solution: [p1051_heightchecker.cpp](../../leetcode/p1051_heightchecker.cpp)
|
||||
- [Leetcode problem 1662: Check If Two String Arrays are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/)
|
||||
|
||||
17
leetcode/p905_sortarraybyparity.cpp
Normal file
17
leetcode/p905_sortarraybyparity.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
// the less than function
|
||||
bool comp(int A, int B){
|
||||
if(A%2==0 && B%2!=0){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
|
||||
vector<int> sortArrayByParity(vector<int>& nums) {
|
||||
sort(nums.begin(),nums.end(),comp);
|
||||
return nums;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user