Skip to content

Commit 432bee4

Browse files
committed
Updated chapter Sorting
1 parent 13ba26c commit 432bee4

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

C++/chapSorting.tex

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ \subsubsection{代码}
2121
// 时间复杂度O(m+n),空间复杂度O(1)
2222
class Solution {
2323
public:
24-
void merge(int A[], int m, int B[], int n) {
24+
void merge(vector<int>& A, int m, vector<int>& B, int n) {
2525
int ia = m - 1, ib = n - 1, icur = m + n - 1;
2626
while(ia >= 0 && ib >= 0) {
2727
A[icur--] = A[ia] >= B[ib] ? A[ia--] : B[ib--];
@@ -98,6 +98,7 @@ \subsubsection{代码}
9898
\begin{Code}
9999
//LeetCode, Merge k Sorted Lists
100100
// 时间复杂度O(n1+n2+...),空间复杂度O(1)
101+
// TODO: 会超时
101102
class Solution {
102103
public:
103104
ListNode *mergeKLists(vector<ListNode *> &lists) {
@@ -273,16 +274,17 @@ \subsubsection{代码}
273274
// 时间复杂度O(n),空间复杂度O(1)
274275
class Solution {
275276
public:
276-
int firstMissingPositive(int A[], int n) {
277-
bucket_sort(A, n);
277+
int firstMissingPositive(vector<int>& nums) {
278+
bucket_sort(nums);
278279

279-
for (int i = 0; i < n; ++i)
280-
if (A[i] != (i + 1))
280+
for (int i = 0; i < nums.size(); ++i)
281+
if (nums[i] != (i + 1))
281282
return i + 1;
282-
return n + 1;
283+
return nums.size() + 1;
283284
}
284285
private:
285-
static void bucket_sort(int A[], int n) {
286+
static void bucket_sort(vector<int>& A) {
287+
const int n = A.size();
286288
for (int i = 0; i < n; i++) {
287289
while (A[i] != i + 1) {
288290
if (A[i] <= 0 || A[i] > n || A[i] == A[A[i] - 1])
@@ -337,10 +339,10 @@ \subsubsection{代码1}
337339
// 时间复杂度O(n),空间复杂度O(1)
338340
class Solution {
339341
public:
340-
void sortColors(int A[], int n) {
342+
void sortColors(vector<int>& A) {
341343
int counts[3] = { 0 }; // 记录每个颜色出现的次数
342344

343-
for (int i = 0; i < n; i++)
345+
for (int i = 0; i < A.size(); i++)
344346
counts[A[i]]++;
345347

346348
for (int i = 0, index = 0; i < 3; i++)
@@ -358,9 +360,9 @@ \subsubsection{代码2}
358360
// 双指针,时间复杂度O(n),空间复杂度O(1)
359361
class Solution {
360362
public:
361-
void sortColors(int A[], int n) {
363+
void sortColors(vector<int>& A) {
362364
// 一个是red的index,一个是blue的index,两边往中间走
363-
int red = 0, blue = n - 1;
365+
int red = 0, blue = A.size() - 1;
364366

365367
for (int i = 0; i < blue + 1;) {
366368
if (A[i] == 0)
@@ -382,9 +384,9 @@ \subsubsection{代码3}
382384
// 时间复杂度O(n),空间复杂度O(1)
383385
class Solution {
384386
public:
385-
void sortColors(int A[], int n) {
386-
partition(partition(A, A + n, bind1st(equal_to<int>(), 0)), A + n,
387-
bind1st(equal_to<int>(), 1));
387+
void sortColors(vector<int>& nums) {
388+
partition(partition(nums.begin(), nums.end(), bind1st(equal_to<int>(), 0)),
389+
nums.end(), bind1st(equal_to<int>(), 1));
388390
}
389391
};
390392
\end{Code}
@@ -397,9 +399,9 @@ \subsubsection{代码4}
397399
// 时间复杂度O(n),空间复杂度O(1)
398400
class Solution {
399401
public:
400-
void sortColors(int A[], int n) {
401-
partition(partition(A, A + n, bind1st(equal_to<int>(), 0)), A + n,
402-
bind1st(equal_to<int>(), 1));
402+
void sortColors(vector<int>& nums) {
403+
partition(partition(nums.begin(), nums.end(), bind1st(equal_to<int>(), 0)),
404+
nums.end(), bind1st(equal_to<int>(), 1));
403405
}
404406
private:
405407
template<typename ForwardIterator, typename UnaryPredicate>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy