Skip to content
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;
}
};