Skip to content

更新了[lcof-03.数组中重复的数字]和[lcof-06.从尾到头打印链表]的cpp解法 #342

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 3, 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
24 changes: 24 additions & 0 deletions lcof/面试题03. 数组中重复的数字/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,30 @@ func findRepeatNumber(nums []int) int {
}
```

### **C++**

```cpp
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
int len = nums.size();
for (int i = 0; i < len; i++) {
while (i != nums[i]) {
// 这一位的值,不等于这一位的数字
if (nums[i] == nums[nums[i]]) {
// 如果在交换的过程中,发现了相等的数字,直接返回
return nums[i];
}

swap(nums[i], nums[nums[i]]);
}
}

return 0;
}
};
```

### **...**

```
Expand Down
19 changes: 19 additions & 0 deletions lcof/面试题03. 数组中重复的数字/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
int len = nums.size();
for (int i = 0; i < len; i++) {
while (i != nums[i]) {
// 这一位的值,不等于这一位的数字
if (nums[i] == nums[nums[i]]) {
// 如果在交换的过程中,发现了相等的数字,直接返回
return nums[i];
}

swap(nums[i], nums[nums[i]]);
}
}

return 0;
}
};
33 changes: 33 additions & 0 deletions lcof/面试题06. 从尾到头打印链表/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,39 @@ func reversePrint(head *ListNode) []int {
}
```

### **C++**

```cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<int> ret;

void getVal(ListNode* head) {
// 这里可以看成是一个节点的树
if (head) {
if (head->next) {
getVal(head->next);
}
ret.push_back(head->val);
}
}

vector<int> reversePrint(ListNode* head) {
getVal(head);
// 返回的是全局的ret信息。在getVal函数中被赋值
return ret;
}
};
```

### **...**

```
Expand Down
27 changes: 27 additions & 0 deletions lcof/面试题06. 从尾到头打印链表/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/

class Solution {
public:
vector<int> ret;

void getVal(ListNode* head) {
if (head) {
if (head->next) {
getVal(head->next);
}
ret.push_back(head->val);
}
}

vector<int> reversePrint(ListNode* head) {
getVal(head);
return ret;
}
};