Skip to content

增加了[lcof-18.删除链表的节点]和[lcof-40.最小的k个数]的cpp解法 #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lcof/面试题18. 删除链表的节点/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,45 @@ func deleteNode(head *ListNode, val int) *ListNode {
}
```

### **C++**

```cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
ListNode* cur = head;
if (!head) {
return nullptr;
}

if (head->val == val) {
// 第一个就匹配的情况
return head->next;
}

while (cur && cur->next) {
if (cur->next->val == val) {
// 如果找到了,直接指向下一个
cur->next = cur->next->next;
break;
} else {
cur = cur->next;
}
}

return head;
}
};
```

### **...**

```
Expand Down
34 changes: 34 additions & 0 deletions lcof/面试题18. 删除链表的节点/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
ListNode* cur = head;
if (!head) {
return nullptr;
}

if (head->val == val) {
// 第一个就匹配的情况
return head->next;
}

while (cur && cur->next) {
if (cur->next->val == val) {
// 如果找到了,直接指向下一个
cur->next = cur->next->next;
break;
} else {
cur = cur->next;
}
}

return head;
}
};
48 changes: 48 additions & 0 deletions lcof/面试题40. 最小的k个数/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,54 @@ var getLeastNumbers = function (arr, k) {
};
```

### **C++**

```cpp
class Solution {
public:
int partition(vector<int>& arr, int begin, int end) {
int l = begin;
int r = end;
int povit = arr[begin];

while (l < r) {
// 从右边开始,找到第一个小于povit的数字(用于交换)
while (l < r && arr[r] >= povit) { r--; }
while (l < r && arr[l] <= povit) { l++; }
if (l < r) { swap(arr[l], arr[r]); }
}

swap(arr[begin], arr[l]);
return l;
}

void partSort(vector<int>& arr, int begin, int end, int target) {
if (begin >= end) {
return;
}

// 思路类似快排,这样做比堆排序时间复杂度低
// C++中,stl提供partial_sort()方法,就是这种实现方式
int mid = partition(arr, begin, end);
if (mid == target) {
return;
} else if (target < mid) {
partSort(arr, begin, mid - 1, target);
} else {
partSort(arr, mid + 1, end, target);
}

return;
}

vector<int> getLeastNumbers(vector<int>& arr, int k) {
partSort(arr, 0, arr.size() - 1, k - 1);
vector<int> ret(arr.begin(), arr.begin() + k);
return ret;
}
};
```

### **...**

```
Expand Down
43 changes: 43 additions & 0 deletions lcof/面试题40. 最小的k个数/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution {
public:
int partition(vector<int>& arr, int begin, int end) {
int l = begin;
int r = end;
int povit = arr[begin];

while (l < r) {
// 从右边开始,找到第一个小于povit的数字(用于交换)
while (l < r && arr[r] >= povit) { r--; }
while (l < r && arr[l] <= povit) { l++; }
if (l < r) { swap(arr[l], arr[r]); }
}

swap(arr[begin], arr[l]);
return l;
}

void partSort(vector<int>& arr, int begin, int end, int target) {
if (begin >= end) {
return;
}

// 思路类似快排,这样做比堆排序时间复杂度低
// C++中,stl提供partial_sort()方法,就是这种实现方式
int mid = partition(arr, begin, end);
if (mid == target) {
return;
} else if (target < mid) {
partSort(arr, begin, mid - 1, target);
} else {
partSort(arr, mid + 1, end, target);
}

return;
}

vector<int> getLeastNumbers(vector<int>& arr, int k) {
partSort(arr, 0, arr.size() - 1, k - 1);
vector<int> ret(arr.begin(), arr.begin() + k);
return ret;
}
};