Commit 196b37e2 by 李楚霏

week4;按题型分类

parent f0c74ea7
var minMutation = function(start, end, bank) {
let used = new Set(bank);
let level = 0;
if(!used.has(end)) return -1;
let queue = [];
queue.push([start, 0]);
let gene = ['A', 'C', 'G', 'T'];
while(queue.length > 0) {
let [currStr, count] = queue.shift();
if(currStr === end) return count;
for(let i =0; i < currStr.length; i++) {
for(let j =0; j <gene.length; j++) {
if(currStr[i] !== gene[j]) {
let d = currStr.slice(0, i) + gene[j] +currStr.slice(i+1);
console.error(`d: ${d}`);
if(used.has(d)) {
queue.push([d, count+1]);
used.delete(d);
}
}
}
}
}
return -1;
};
let start = "AACCGGTT";
let end = "AACCGGTA";
let bank = ["AACCGGTA"];
minMutation(start, end, bank);
// console.log(nums);
var permute = function(nums) {
let visited = [];
let path = [];
let res=[];
if (nums.length < 0){
return res;
}
//sort用法弄错,这题重点就是要先排序!!!
nums.sort((a,b) => a - b);
const dfs = (visited, path) => {
if (path.length == nums.length) {
console.error(`遍历完的路径${path}`);
res.push(path.slice());
}
for(let i =0; i< nums.length; i++) {
if (visited[i]) continue;
if(i>0 && nums[i] === nums[i-1] && !visited[i-1]) continue;
path.push(nums[i]);
console.error(`开始遍历的路径${path}`);
visited[i] = true;
dfs(visited.slice(), path.slice());
path.pop();
console.error(`开始回溯的路径${path}`);
visited[i] = false;
}
}
dfs(visited, path);
return res;
};
let nums = [3,3,0,3];
let res = permute(nums);
console.log(res);
/**
/**
* 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:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*>qu;
vector<vector<int>>res;
if(root == nullptr) {
return res;
}
qu.push(root);
while(!qu.empty()) {
int size = qu.size();
vector<int> tmp;
for(int i =0; i < size; i++) {
TreeNode* node = qu.front();
qu.pop();
tmp.push_back(node->val);
if(node->left != nullptr) {
qu.push(node->left);
}
if(node->right != nullptr) {
qu.push(node->right);
}
}
res.push_back(tmp);
}
return res;
}
};
\ No newline at end of file
### 第四周学习总结
### 第四周学习总结
#### 深度优先搜索、广度优先搜索
- 深度优先搜索
- 一般用递归
- 广度优先搜索
- 可以用递归或者非递归,递归一般是使用队列;非递归则是要使用栈来模拟
- 题目总结:
- 2叉树层序遍历,基因序列
#### 贪心算法
- 贪心算法是一种从局部找最优解的算法,它不可以回溯。
- 特殊的求解方法,从前往后贪心或者从后往前贪心;
- 题目总结:
- 柠檬水找零
- 买卖股票的最佳时机ii
#### 二分查找
- 寻找一个中间值,从中间值分为左右两边去求解,以此类推。
\ No newline at end of file
/**思路:贪心
/**思路:贪心
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
let benifit =0;
for(let i = 0; i < prices.length; i++) {
let j = i-1;
let tmp = prices[i] - prices[j];
if(tmp> 0) {
benifit += tmp;
}
}
return benifit;
};
class Solution {
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
int size = bills.size();
int five = 0;
int ten =0;
for(int i = 0; i<size;i++) {
if(bills[i] == 5) {
five ++;
} else if(bills[i] == 10) {
if (five == 0) {
return false;
}
five--;
ten++;
} else {
if (five >0 && ten >0) {
five--;
ten--;
} else if(five >= 3) {
five -=3;
} else {
return false;
}
}
}
return true;
}
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment