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
49 changes: 49 additions & 0 deletions lcof/面试题57 - II. 和为s的连续正数序列/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,55 @@ var findContinuousSequence = function (target) {
};
```

### **C++**

```cpp
class Solution {
public:
vector<int> build(int small, int big) {
vector<int> ret;
for (int i = small; i <= big; i++) {
ret.push_back(i);
}

return ret;
}

vector<vector<int>> findContinuousSequence(int target) {
vector<vector<int>> ret;
int small = 1;
int big = 2;
int mid = (target + 1) / 2;
int curSum = small + big;

if (target < 3) {
ret;
}

while(small < mid) {
if (curSum == target) {
ret.push_back(build(small, big));
}

while (curSum > target && small < mid) {
// 一直减去,减去到比target小停止
curSum -= small;
small++;

if (curSum == target && small < mid) {
ret.push_back(build(small, big));
}
}

big++;
curSum += big;
}

return ret;
}
};
```

### **...**

```
Expand Down
44 changes: 44 additions & 0 deletions lcof/面试题57 - II. 和为s的连续正数序列/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution {
public:
vector<int> build(int small, int big) {
vector<int> ret;
for (int i = small; i <= big; i++) {
ret.push_back(i);
}

return ret;
}

vector<vector<int>> findContinuousSequence(int target) {
vector<vector<int>> ret;
int small = 1;
int big = 2;
int mid = (target + 1) / 2;
int curSum = small + big;

if (target < 3) {
ret;
}

while(small < mid) {
if (curSum == target) {
ret.push_back(build(small, big));
}

while (curSum > target && small < mid) {
// 一直减去,减去到比target小停止
curSum -= small;
small++;

if (curSum == target && small < mid) {
ret.push_back(build(small, big));
}
}

big++;
curSum += big;
}

return ret;
}
};
38 changes: 38 additions & 0 deletions lcof/面试题68 - II. 二叉树的最近公共祖先/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,44 @@ var lowestCommonAncestor = function (root, p, q) {
};
```

### **C++**

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/

class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
// 如果找到val,层层向上传递该root
if (nullptr == root || p->val == root->val || q->val == root->val) {
return root;
}

TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);

if (left != nullptr && right != nullptr) {
// 如果两边都可以找到
return root;
} else if (left == nullptr) {
// 如果左边没有找到,则直接返回右边内容
return right;
} else {
return left;
}
}
};

```

### **...**

```
Expand Down
32 changes: 32 additions & 0 deletions lcof/面试题68 - II. 二叉树的最近公共祖先/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/

class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
// 如果找到val,层层向上传递该root
if (nullptr == root || p->val == root->val || q->val == root->val) {
return root;
}

TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);

if (left != nullptr && right != nullptr) {
// 如果两边都可以找到
return root;
} else if (left == nullptr) {
// 如果左边没有找到,则直接返回右边内容
return right;
} else {
return left;
}
}
};