验证括号字符串:
用了两个栈来存放。
只需要考虑) 优先用 ( 其次用* 即可
#include <bits/stdc++.h>
using namespace std;
bool checkValidString(char* s){
int len = strlen(s);
stack<int> left_stack,star_stack;
for (int i = 0; i < len; i++)
{
if(s[i] == '('){
left_stack.push(i);
}
else if(s[i] == ')'){
if (!left_stack.empty()){
left_stack.pop();
}
else if(!star_stack.empty()){
star_stack.pop();
}
else{
return false;
}
}
else if(s[i] == '*'){
star_stack.push(i);
}
}
//处理栈中的符号:
while(!left_stack.empty() && !star_stack.empty()){
if(left_stack.top() > star_stack.top()){
return false;
}
left_stack.pop();
star_stack.pop();
}
return left_stack.empty();
}
int main() {
char s[105];
fgets(s,105,stdin);
if(checkValidString(s)){
printf("can do it");
}
else{
printf("can not do it");
}
}
岛屿的数目:
给定一张2d网格地图,1代表陆地,0代表水域,计算岛屿的数量。
一个岛屿被水包围,由水平或垂直连接相邻的陆地形成。
你可以假设网格的四个边都被水包围。
Example 1:Input:
11110
11010
11000
00000 Output: 1
Example 1:
Input:
11000
11000
00100
00011 Output: 3
深度优先遍历:
首先把当前的水域设置为0,再把上下左右坐标求出分别调用dfs算法
#include <stdio.h>
// 定义网格的行和列
#define ROWS 4
#define COLS 5
// 方向数组:上下左右
int directions[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
// 深度优先搜索(DFS)函数
void dfs(char grid[ROWS][COLS], int x, int y) {
// 如果超出边界,或者当前位置是水域'0',直接返回
if (x < 0 || x >= ROWS || y < 0 || y >= COLS || grid[x][y] == '0') {
return;
}
// 将当前陆地标记为已访问(水域'0')
grid[x][y] = '0';
// 遍历上下左右四个方向
for (int i = 0; i < 4; i++) {
int newX = x + directions[i][0];
int newY = y + directions[i][1];
dfs(grid, newX, newY); // 递归搜索相邻的陆地
}
}
// 主函数:计算岛屿数量
int numIslands(char grid[ROWS][COLS]) {
int islandCount = 0;
// 遍历整个网格
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
// 如果当前元素是陆地'1',说明发现了一个新的岛屿
if (grid[i][j] == '1') {
islandCount++;
// 从该陆地块开始进行DFS,标记所有连通的陆地为'0'
dfs(grid, i, j);
}
}
}
return islandCount;
}
// 打印网格(用于调试)
void printGrid(char grid[ROWS][COLS]) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("%c", grid[i][j]);
}
printf("\n");
}
}
int main() {
// 示例1
char grid1[ROWS][COLS] = {
{'1', '1', '1', '1', '0'},
{'1', '1', '0', '1', '0'},
{'1', '1', '0', '0', '0'},
{'0', '0', '0', '0', '0'}
};
// 输出岛屿数量
printf("岛屿数量: %d\n", numIslands(grid1));
// 示例2
char grid2[ROWS][COLS] = {
{'1', '1', '0', '0', '0'},
{'1', '1', '0', '0', '0'},
{'0', '0', '1', '0', '0'},
{'0', '0', '0', '1', '1'}
};
// 输出岛屿数量
printf("岛屿数量: %d\n", numIslands(grid2));
return 0;
}
最小路径和(数组元素的距离):
最左边含有第一个1的列:
int findFirstOne (struct BinaryMatrix* matrix,int row) {
int m = matrix->dimenstion(martic)[1];
int start = 0,end = m;
while (true)
{
if (start == end) return -1;
{
int mid = (start + end) / 2;
}
if (matrix->get(matrix, row, mid) == 1){
if (mid == 0)
{
return 0;
}
if (matrix->get(matrix, row, mid-1) == 0)
{
return mid;
} else{
end = mid;
}
} else {
start = mid + 1;
}
}
return -1;
}
int leftMostColumnWithOne(struct BinaryMatrix* matrix) {
int n = matrix->dimenstion(matrix)[0];
int leftMostCol = -1;
for (int row = 0; row < n; row++)
{
int leftCol = findFirstOne(matrix, row);
if (leftCol != -1)
{
if (leftMostCol == -1 || leftCol < leftMostCol)
{
leftMostCol = leftCol;
}
}
}
return leftMostCol;
}