mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-11-24 13:03:41 +00:00
Compare commits
No commits in common. "0eb1fb24b71f93a096bf431bba40e19f7c0198d4" and "3cdbea167c928b00aefe340184d5ebdaf6367477" have entirely different histories.
0eb1fb24b7
...
3cdbea167c
@ -60,8 +60,6 @@
|
||||
|
||||
**题目代码**
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int findMin(int[] nums) {
|
||||
@ -87,27 +85,3 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
C++ Code:
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
int findMin(vector <int> & nums) {
|
||||
int left = 0;
|
||||
int right = nums.size() - 1;
|
||||
while (left < right) {
|
||||
if (nums[left] < nums[right]) {
|
||||
return nums[left];
|
||||
}
|
||||
int mid = left + ((right - left) >> 1);
|
||||
if (nums[left] > nums[mid]) {
|
||||
right = mid;
|
||||
} else {
|
||||
left = mid + 1;
|
||||
}
|
||||
}
|
||||
return nums[left];
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
@ -350,8 +350,6 @@ class Solution {
|
||||
|
||||
**测试题目: leetcode 102 二叉树的层序遍历**
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public List<List<Integer>> levelOrder(TreeNode root) {
|
||||
@ -380,33 +378,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
C++ Code:
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> levelOrder(TreeNode* root) {
|
||||
vector<vector<int>> res;
|
||||
if (!root) return res;
|
||||
queue<TreeNode *> q;
|
||||
q.push(root);
|
||||
while (!q.empty()) {
|
||||
vector <int> t;
|
||||
int size = q.size();
|
||||
for (int i = 0; i < size; ++i) {
|
||||
TreeNode * temp = q.front();
|
||||
q.pop();
|
||||
if (temp->left != nullptr) q.push(temp->left);
|
||||
if (temp->right != nullptr) q.push(temp->right);
|
||||
t.emplace_back(temp->val);
|
||||
}
|
||||
res.push_back(t);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
时间复杂度:O(n) 空间复杂度:O(n)
|
||||
|
||||
大家如果吃透了二叉树的层序遍历的话,可以顺手把下面几道题目解决掉,思路一致,甚至都不用拐弯
|
||||
|
Loading…
Reference in New Issue
Block a user