AI-CV-II arithmetic Tips
*此文章中算法来自leetcode、lintcode、牛客等…
*其中包括了算法工程师工作中遇到的问题
*大型互联网公司面试经典考题
*神经网路中的基础算法思想
*难度一般为中级到高级。
1.number-of-islands
1.1实现功能:
Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. If two 1 is adjacent, we consider them in the same island. We only consider up/down/left/right adjacent.
Find the number of islands.
Example
Example 1:
Input:
[
[1,1,0,0,0],
[0,1,0,0,1],
[0,0,0,1,1],
[0,0,0,0,0],
[0,0,0,0,1]
]
Output:
3
Example 2:
Input:
[
[1,1]
]
Output:
1
1.2题目来源
题目来源(可点击):Leetcode-Problems
1.3代码实现
代码块如下:
//C++实现
//dfs是深度优先算法,bfs是广度优先算法
class Solution {
private: //引用,在此处改变grid,也会改变下面函数中得grid
void dfs(vector<vector<bool>>& grid, int r, int c){
if(r <0 || r >= grid.size() || c < 0 || c >= grid[0].size() || !grid[r][c]){
return;
}
grid[r][c] = false;
dfs(grid, r + 1, c);
dfs(grid, r - 1, c);
dfs(grid, r, c + 1);
dfs(grid, r, c - 1);
return;
}
public:
/**
* @param grid: a boolean 2D matrix
* @return: an integer
*/
int numIslands(vector<vector<bool>> &grid) {
// write your code here
//dfs方法
int rows = grid.size();
if (!rows) return 0;
int cols = grid[0].size();
if (!cols) return 0;
int num = 0;
for(int r = 0; r < rows; ++r){
for(int c = 0 ;c < cols; ++c){
if(grid[r][c]){
dfs(grid, r, c);
++num;
}
}
}
return num;
}
};
1.4结果演示
Lintcode结果演示:
Input
Show Difference
[[1,1,0,0,0],[0,1,0,0,1],[0,0,0,1,1],[0,0,0,0,0],[0,0,0,0,1]]
Output
3
Expected
3
1.5 基本思想
基本思想:DFS
2.number-of-islands-ii
2.1实现功能:
Given a n,m which means the row and column of the 2D matrix and an array of pair A( size k). Originally, the 2D matrix is all 0 which means there is only sea in the matrix. The list pair has k operator and each operator has two integer A[i].x, A[i].y means that you can change the grid matrix[A[i].x][A[i].y] from sea to island. Return how many island are there in the matrix after each operator.
Example
Example 1:
Input: n = 4, m = 5, A = [[1,1],[0,1],[3,3],[3,4]]
Output: [1,1,2,2]
Explanation:
0. 00000
00000
00000
00000
- 00000
01000
00000
00000 - 01000
01000
00000
00000 - 01000
01000
00000
00010 - 01000
01000
00000
00011
Example 2:
Input: n = 3, m = 3, A = [[0,0],[0,1],[2,2],[2,1]]
Output: [1,1,2,2]
Notice
0 is represented as the sea, 1 is represented as the island. If two 1 is adjacent, we consider them in the same island. We only consider up/down/left/right adjacent.
2.2题目来源
题目来源(可点击):Leetcode-Problems
2.3代码实现
代码块如下:
//C++实现
/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
class Solution {
public:
/**
* @param n: An integer
* @param m: An integer
* @param operators: an array of point
* @return: an integer array
*/
vector<int> numIslands2(int n, int m, vector<Point> &operators) {
// write your code here
//bfs 广度优先搜索;union find并查集 ;路径压缩
vector<int>res;
if(!(n * m)) return res; //判断n,m不空
vector<int>roots(n * m, -1); //每一个Barnaby元素都是一个根,根得值位=为-1,即空根
vector<pair<int, int>>dir = {{1,0}, {-1, 0}, {0, 1},{0, -1}};//bfs四个方向,下上右左
unordered_set<int>us; //用来查重
int num = 0;
for(auto p : operators){
int r = p.x;
int c = p.y;
int id = r * m + c; //给每一个元素赋id值
roots[id] = id; //让root[id] 等于id
if(us.find(id) != us.end()){ //判断输入是不是新的,如果不是新的,继续for循环
res.push_back(num);
continue;
}
us.insert(id); //如果是新的id,把它插入到unordered set中
++num; //把新id值加1
for(auto d :dir){
int nr = r + d.first; //.first和.secod代表{1,0}等方向坐标
int nc = c+ d.second;
int nid = nr * m + nc;
if(nr >= 0 && nr < n && nc >=0 && nc < m && roots[nid] != -1){
while(id != roots[id]){ //union find操作
roots[id] = roots[roots[id]]; //路径压缩;youtube查
id = roots[id];
}
while(nid != roots[nid]){ //union find操作
roots[nid] = roots[roots[nid]];
nid = roots[nid];
}
if(roots[nid] != roots[id]){
roots[nid] = roots[id];
--num;
}
}
}
res.push_back(num);
}
return res;
}
};
2.4结果演示
Lintcode结果演示:
Accepted
Powered by LintCode FlashJudge
Input
4
5
[[1,1],[0,1],[3,3],[3,4]]
Output
[1,1,2,2]
Expected
[1,1,2,2]
2.5 基本思想
基本思想:BFS。