-
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
2f68211
commit 42d2bc5
Showing
6 changed files
with
82 additions
and
11 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 @@ | ||
# Algorithm Exercise of the book: *Coding Interviews: Questions, Analysis and Solutions* |
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
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,36 @@ | ||
#include "solution.h" | ||
|
||
/* | ||
* 21. 调整数组顺序使奇数位于偶数前面 | ||
* 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数在数组的前半部分,所有偶数在数组的后半部分。 | ||
*/ | ||
|
||
vector<int> solution::Reorder(vector<int>& nums) { | ||
if (nums.empty()) { | ||
return nums; | ||
} | ||
|
||
auto pBegin = nums.begin(); | ||
auto pEnd = --nums.end(); | ||
auto IsOdd = [](int num) -> bool { | ||
return num % 2 == 1; | ||
}; | ||
|
||
while (pBegin < pEnd) { | ||
while (pBegin < pEnd && IsOdd(*pBegin)) { | ||
pBegin++; | ||
} | ||
|
||
while (pBegin < pEnd && !IsOdd(*pEnd)) { | ||
pEnd--; | ||
} | ||
|
||
if (pBegin < pEnd) { | ||
int temp = *pBegin; | ||
*pBegin = *pEnd; | ||
*pEnd = temp; | ||
} | ||
} | ||
|
||
return nums; | ||
} |
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
This file was deleted.
Oops, something went wrong.
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