/*问题1
给一个01矩阵,1代表是陆地,0代表海洋, 如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。
岛屿: 相邻陆地可以组成一个岛屿(相邻:上下左右) 判断岛屿个数。
例如:
[1,1,0,0,0],
[0,1,0,1,1],
[0,0,0,1,1],
[0,0,0,0,0],
[0,0,1,1,1]
对应的输出为3
*/
struct pos_t {
int row, col;
pos_t(int r, int c):row(r), col(c){}
};
int solve(vector<vector<char>>& grid) {
// write code here
int rows = grid.size();
int cols = grid[0].size();
int num = 0;
vector<vector<int>> flag(rows, vector<int>(cols, 0));
queue<pos_t> que;
while (true) {
bool targ = false;
// 找到第一个岛屿
for (int i = 0; i < rows && !targ; ++i) {
for (int j = 0; j < cols; ++j) {
if (flag[i][j] == 0 && grid[i][j] == '1') {
que.push(pos_t(i, j));
flag[i][j] = 1;
targ = true;
++num;
break;
}
}
}
if (que.empty()) break;
while (!que.empty()) {
pos_t pos = que.front();
int size = que.size();
// 上下左右
if (pos.row - 1 >= 0 && flag[pos.row - 1][pos.col] == 0 && grid[pos.row - 1][pos.col] == '1') {// 上
pos_t n(pos.row - 1, pos.col);
flag[n.row][n.col] = 1;
que.push(n);
}
else if (pos.row + 1 < rows && flag[pos.row + 1][pos.col] == 0 && grid[pos.row + 1][pos.col] == '1') {// 下
pos_t n(pos.row + 1, pos.col);
flag[n.row][n.col] = 1;
que.push(n);
}
else if (pos.col - 1 >= 0 && flag[pos.row][pos.col - 1] == 0 && grid[pos.row][pos.col - 1] == '1') {// 左
pos_t n(pos.row, pos.col - 1);
flag[n.row][n.col] = 1;
que.push(n);
}
else if (pos.col + 1 < cols && flag[pos.row][pos.col + 1] == 0 && grid[pos.row][pos.col + 1] == '1') {// 右
pos_t n(pos.row, pos.col + 1);
flag[n.row][n.col] = 1;
que.push(n);
}
else {
que.pop();
}
}
}
return num;
}
/*问题2
树的右视图
*/
vector<int> solve(vector<int>& xianxu, vector<int>& zhongxu) {
// write code here
int len_x = xianxu.size();
int len_z = zhongxu.size();
vector<int> ret;
if (len_x != len_z || len_x == 0) return ret;
if (len_x == 1) {
ret.emplace_back(xianxu[0]);
return ret;
}
TreeNode* tree = NULL;
createTree(xianxu, 0, len_x, zhongxu, 0, tree);
queue<TreeNode*> que;
TreeNode* node = NULL;
que.push(tree);
queue<TreeNode*> tmp_level;
vector<vector<int>> levels;
while (true) {
vector<int> level;
while (!que.empty()) {
node = que.front();
if (node->left) tmp_level.push(node->left);
if (node->right) tmp_level.push(node->right);
level.emplace_back(node->val);
que.pop();
}
levels.emplace_back(level);
if (tmp_level.empty()) break;
while (!tmp_level.empty()) {
que.push(tmp_level.front());
tmp_level.pop();
}
}
for (int i = 0; i < levels.size(); ++i) {
ret.emplace_back(levels[i][levels[i].size()-1]);
}
return ret;
}
/*问题3
输入一个长度为 n 字符串,打印出该字符串中字符的所有排列,你可以以任意顺序返回这个字符串数组。
例如输入字符串ABC,则输出由字符A,B,C所能排列出来的所有字符串ABC,ACB,BAC,BCA,CBA和CAB。
数据范围:n < 10
要求:空间复杂度 O(n),时间复杂度 O(n)
输入:"abc"
返回值:["abc","acb","bac","bca","cab","cba"]
*/
vector<string> Permutation(string str) {
vector<string> ret;
int len = str.size();
if (len == 0) return ret;
if (len == 1) {
ret.emplace_back(str);
return ret;
}
map<int, int> map, tmp_map;// key:"a" val: 数量
queue<string> q;
string tmp_str;
for (int i = 0; i < len; ++i) {
++map[str[i] - 'a'];
q.push(str.substr(i, 1));
}
int lenth = 0;
while (1 == 1) {
tmp_map = map;
tmp_str = q.front();
if (tmp_str.size() == len) break;
lenth = tmp_str.size();
for (int i = 0; i < lenth; ++i) {
--tmp_map[tmp_str[i] - 'a'];
}
for (auto iter = tmp_map.begin(); iter != tmp_map.end(); ++iter) {
if ((*iter).second != 0) {
string str(1, (*iter).first + 'a');
str = tmp_str + str;
q.push(str);
}
}
q.pop();
}
set<string> filter;
while (!q.empty()) {
filter.insert(q.front());
q.pop();
}
for (auto iter = filter.begin(); iter != filter.end(); ++iter) {
ret.emplace_back((*iter).c_str());
}
return ret;
}
/*问题4
给定一个二叉树,返回该二叉树的之字形层序遍历,(第一层从左向右,下一层从右向左,一直这样交替)
1
2 3 ==> 打印出来 1 3 2 4 5 6 7
4 5 6 7
*/
vector<vector<int>> Print(TreeNode* pRoot) {
vector<vector<int>> ret;
if (!pRoot) {
return ret;
}
int flag = 1; // 基数左到右 入栈 偶数 右到左入栈
stack<TreeNode*> st;
queue<TreeNode*> queue;
queue.push(pRoot);
TreeNode* node = NULL;
while (1 == 1) {
vector<int> level; // 每一层的元素
while (!queue.empty()) {
node = queue.front();
if (flag % 2 != 0) {// 基数
if (node->left) st.push(node->left);
if (node->right) st.push(node->right);
}
else {// 偶数
if (node->right) st.push(node->right);
if (node->left) st.push(node->left);
}
level.push_back(node->val);
queue.pop();
}
ret.push_back(level);
if (st.empty()) break;
++flag;
while(!st.empty()){
queue.push(st.top());
st.pop();
}
}
return ret;
}
|