Skip to content

Commit f71118e

Browse files
committed
add code2interview 剑指offer
1 parent 04fe7d4 commit f71118e

File tree

59 files changed

+3280
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+3280
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
#include <utility>
3+
#include <vector>
4+
#include <unordered_map>
5+
using namespace std;
6+
7+
// 每次都可以先自己定义一个 MyException
8+
class MyException: public exception {
9+
public:
10+
std::string s;
11+
explicit MyException(std::string str): s(std::move(str)) {};
12+
const char* what() const noexcept override {return s.c_str();};
13+
};
14+
15+
16+
17+
class Solution {
18+
public:
19+
int findRepeatNumber(vector<int>& nums) {
20+
for (std::size_t i = 0; i < nums.size(); i++) {
21+
while (nums[i] != i) {
22+
if (nums[nums[i]] != nums[i])
23+
std::swap(nums[i], nums[nums[i]]);
24+
else
25+
return nums[i];
26+
}
27+
}
28+
// 如果满足题目要求,这里不会到达
29+
// throw MyException("不能运行到这里");
30+
return -1;
31+
}
32+
33+
int findRepeatNumber2(vector<int>& nums) {
34+
// 可以直接使用 hash map
35+
unordered_map<int, bool> m;
36+
for (auto i: nums) {
37+
if (m.find(i) == m.end()) {
38+
m[i] = true;
39+
} else
40+
return i;
41+
}
42+
throw MyException("不可能到达这里");
43+
}
44+
};
45+
46+
int main() {
47+
vector<int> nums {0, 2, 2};
48+
cout << Solution::findRepeatNumber(nums) << endl;
49+
cout << Solution::findRepeatNumber2(nums) << endl;
50+
return 0;
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
6+
class Solution {
7+
public:
8+
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
9+
// 直接暴力遍历每一个数字
10+
bool flag = false;
11+
for (const auto& v: matrix) {
12+
// v 是一个 arr
13+
for (const auto& i: v) {
14+
if (i == target) {
15+
flag = true;
16+
}
17+
}
18+
}
19+
return flag;
20+
21+
}
22+
bool findNumberIn2DArray2(vector<vector<int>> &matrix, int target) {
23+
// 这这个使用 数组规律做题
24+
if (matrix.empty()) return false;
25+
int colIdx = 0, rowIdx = int(matrix.size()) - 1;
26+
while (colIdx < matrix[0].size() && rowIdx >= 0) {
27+
if (matrix[rowIdx][colIdx] == target) {
28+
return true;
29+
} else if (matrix[rowIdx][colIdx] > target) {
30+
rowIdx--;
31+
} else {
32+
colIdx++;
33+
}
34+
}
35+
return false;
36+
}
37+
38+
int findNumberIn2DArray3(vector<vector<int>> &matrix, int target) {
39+
// 还可以使用二分法做题
40+
// 这种方法其实并不建议
41+
// TODO: 需要 Debug
42+
43+
if (matrix.empty() or matrix[0].empty())
44+
return false;
45+
46+
int low = 0, high = int(matrix.size()) -1;
47+
int mid;
48+
while (low < high) {
49+
mid = low + (high - low + 1) / 2;
50+
if (matrix[mid][0] > target)
51+
high = mid -1;
52+
else
53+
low = mid;
54+
}
55+
int rowIdx = low;
56+
int left = 0, right = (int)matrix[0].size() -1;
57+
while (left < right) {
58+
mid = left + (right - left + 1) / 2;
59+
if (matrix[rowIdx][mid] > target) {
60+
right = mid -1;
61+
} else
62+
left = mid;
63+
}
64+
// 这个时候 left 一定等于 right
65+
return matrix[rowIdx][left] == target;
66+
}
67+
};
68+
69+
int main() {
70+
Solution solution;
71+
vector<vector<int>> nums {
72+
{1, 4, 7, 11, 15},
73+
{2, 5, 8, 12, 19},
74+
{3, 6, 9, 16, 22},
75+
{10, 13, 14, 17, 24},
76+
{18, 21, 23, 26, 30}};
77+
78+
// vector<vector<int>> nums;
79+
// cout << solution.findNumberIn2DArray(nums, 9) << endl;
80+
// cout << solution.findNumberIn2DArray2(nums, 3) << endl;
81+
cout << solution.findNumberIn2DArray3(nums, 5) << endl;
82+
return 0;
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
6+
class Solution {
7+
public:
8+
string replaceSpace(string s) {
9+
string::size_type pos(0);
10+
while (true) {
11+
if((pos=s.find(' ', pos))!=string::npos) {
12+
s.replace(pos,1,"%20");//替换
13+
pos += 3;
14+
}
15+
else break;
16+
}
17+
return s;
18+
}
19+
20+
string replaceSpace2(string s) {
21+
if (s.empty()) return s;
22+
int spaceCount = 0;
23+
for (const auto & i: s) {
24+
if (i == ' ')spaceCount++;
25+
}
26+
std::size_t oldLen = s.size();
27+
28+
std::size_t newLen = s.size() + spaceCount * 2;
29+
s.resize(newLen);
30+
while (oldLen != newLen) {
31+
--oldLen;
32+
if (s[oldLen] == ' ') {
33+
s[--newLen] = '0';
34+
s[--newLen] = '2';
35+
s[--newLen] = '%';
36+
} else
37+
s[--newLen] = s[oldLen];
38+
}
39+
return s;
40+
}
41+
};
42+
43+
int main() {
44+
Solution solution;
45+
string s = "hello world";
46+
47+
cout << solution.replaceSpace2(
48+
"We are happy.") << endl;
49+
return 0;
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <string>
2+
#include <vector>
3+
#include <algorithm>
4+
#include <stack>
5+
#include <iostream>
6+
using namespace std;
7+
8+
/**
9+
* Definition for singly-linked list.
10+
* struct ListNode {
11+
* int val;
12+
* ListNode *next;
13+
* ListNode(int x) : val(x), next(NULL) {}
14+
* };
15+
*/
16+
17+
18+
struct ListNode {
19+
int val;
20+
ListNode *next;
21+
explicit ListNode(int x) : val(x), next(nullptr) {}
22+
};
23+
24+
/**
25+
* Definition for singly-linked list.
26+
* struct ListNode {
27+
* int val;
28+
* ListNode *next;
29+
* ListNode(int x) : val(x), next(NULL) {}
30+
* };
31+
*/
32+
class Solution {
33+
public:
34+
vector<int> reversePrint(ListNode* head) {
35+
vector<int> res;
36+
stack<int> s;
37+
while(head) {
38+
s.push(head->val);
39+
head = head->next;
40+
}
41+
while (!s.empty()) {
42+
res.push_back(s.top());
43+
s.pop();
44+
}
45+
46+
return res;
47+
}
48+
49+
vector<int> reversePrint2(ListNode *head) {
50+
vector<int> res;
51+
reversePrintRecursive(head, res);
52+
return res;
53+
}
54+
55+
void reversePrintRecursive(ListNode *head, vector<int> &res) {
56+
// 这是一个辅助函数
57+
if (head == nullptr) return;
58+
reversePrintRecursive(head->next, res);
59+
res.push_back(head->val);
60+
}
61+
};
62+
63+
int main() {
64+
Solution solution;
65+
auto *head = new ListNode(0);
66+
auto cur = head;
67+
for (int i = 0; i < 5; i++) {
68+
cur->next = new ListNode(i);
69+
cur = cur->next;
70+
}
71+
cur->next = nullptr;
72+
vector<int> res = solution.reversePrint2(head->next);
73+
for (auto i: res) {
74+
cout << i << endl;
75+
}
76+
return 0;
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <string>
2+
#include <vector>
3+
#include <algorithm>
4+
#include <stack>
5+
#include <iostream>
6+
using namespace std;
7+
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* struct TreeNode {
12+
* int val;
13+
* TreeNode *left;
14+
* TreeNode *right;
15+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
16+
* };
17+
*/
18+
19+
struct TreeNode {
20+
int val;
21+
TreeNode *left;
22+
TreeNode *right;
23+
explicit TreeNode(int x): val(x), left(nullptr), right(nullptr) {}
24+
};
25+
26+
27+
class Solution {
28+
public:
29+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
30+
// 找到中止条件
31+
if (preorder.empty()) return nullptr;
32+
33+
// 首先简历树的根节点
34+
auto root = new TreeNode(preorder[0]);
35+
// 下一步是获取 左子树的 preorder 和 inorder
36+
auto it = std::find(inorder.begin(), inorder.end(), preorder[0]);
37+
38+
vector<int> leftInOrder(inorder.begin(), it);
39+
// 不能包括第一个数字
40+
vector<int> rightInOrder(it + 1, inorder.end());
41+
42+
vector<int> leftPreOrder(preorder.begin() + 1, preorder.begin() + 1 + leftInOrder.size());
43+
vector<int> rightPreOrder(preorder.begin() + 1 + leftInOrder.size(), preorder.end());
44+
45+
root->left = buildTree(leftPreOrder, leftInOrder);
46+
root->right = buildTree(rightPreOrder, rightInOrder);
47+
return root;
48+
}
49+
50+
TreeNode* buildTreeNoRecursive(vector<int>& preorder, vector<int>& inorder) {
51+
if (preorder.empty()) return nullptr;
52+
stack<TreeNode*> roots;
53+
std::size_t pre = 0, in = 0;
54+
55+
auto root = new TreeNode(preorder[pre]);
56+
auto returnRoot = root;
57+
roots.push(root);
58+
pre++;
59+
60+
while (pre < preorder.size()) {
61+
if (root->val == inorder[in]) {
62+
while (!roots.empty() && roots.top()->val == inorder[in]) {
63+
root = roots.top();
64+
roots.pop();
65+
in++;
66+
}
67+
root->right = new TreeNode(preorder[pre]);
68+
root = root->right;
69+
roots.push(root);
70+
pre++;
71+
} else {
72+
root->left = new TreeNode(preorder[pre]);
73+
root = root->left;
74+
roots.push(root);
75+
pre++;
76+
}
77+
}
78+
return returnRoot;
79+
}
80+
81+
};
82+
83+
int main() {
84+
Solution solution;
85+
vector<int> preorder{3,5, 9,10, 20,15,7}, inorder{9,5, 10, 3,15,20,7};
86+
solution.buildTreeNoRecursive(preorder, inorder);
87+
return 0;
88+
}

0 commit comments

Comments
 (0)