当前位置: 首页 > news >正文

盐城微信公众平台网站制作湖南网站建设seo

盐城微信公众平台网站制作,湖南网站建设seo,网站建设完整版,宁夏银川网站建设本系列为笔者的 Leetcode 刷题记录,顺序为 Hot 100 题官方顺序,根据标签命名,记录笔者总结的做题思路,附部分代码解释和疑问解答。 目录 01 矩阵置零 方法一:标记数组 方法二:两个标记变量 02 螺旋矩阵…

本系列为笔者的 Leetcode 刷题记录,顺序为 Hot 100 题官方顺序,根据标签命名,记录笔者总结的做题思路,附部分代码解释和疑问解答。

目录

01 矩阵置零

方法一:标记数组

方法二:两个标记变量

02 螺旋矩阵

方法一:模拟

方法二:按层模拟

03 旋转图像

方法一:辅助数组

方法二:原地旋转

方法三:用翻转代替旋转

04 搜索二维矩阵 Ⅱ

方法一:直接查找

方法二:二分法

方法三:Z字形查找


01 矩阵置零

class Solution {
public:void setZeroes(vector<vector<int>>& matrix) {}
};
方法一:标记数组

时间复杂度 O(mn),空间复杂度 O(m + n)

  • 建立两个数组 row(m)col(n),存储 matrix 中行和列的含零情况

  • 遍历数组,判断行或列含零,执行 matrix[i][j] = 0

class Solution {
public:void setZeroes(vector<vector<int>>& matrix) {int m = matrix.size();int n = matrix[0].size();vector<int> row(m), col(n);for(int i=0; i<m; ++i){for(int j=0; j<n; ++j){if(!matrix[i][j]){row[i] = true;col[j] = true;}}}for(int i=0; i<m; ++i){for(int j=0; j<n; ++j){if(row[i] || col[j]){matrix[i][j] = 0;}}}}
};

vector<int> row(m), col(n);这俩数组没有初始化啥的吗,它们声明时的默认元素值是多少?

使用 vector<int> row(m) 这样的语句声明一个 vector并指定大小时, vector会自动初始化为指定大小的元素,且每个元素默认初始化为零。

方法二:两个标记变量

时间复杂度 O(mn),空间复杂度 O(1)

  • 建立两个标记 flag_col0flag_row0,存储 matrix 中除第零行和第零列的含零情况

  • 遍历数组,判断 !matrix[i][0] || !matrix[0][j],执行 matrix[i][j] = 0

  • 第零行和第零列单独更新

class Solution {
public:void setZeroes(vector<vector<int>>& matrix) {int m = matrix.size(); //一共m行int n = matrix[0].size(); //一共n列int flag_col0 = false, flag_row0 = false;for(int i=0; i<m; ++i){if(!matrix[i][0]) flag_col0 = true;}for(int j=0; j<n; ++j){if(!matrix[0][j]) flag_row0 = true;}//处理大部分元素for(int i=1; i<m; ++i){for(int j=1; j<n; ++j){if(!matrix[i][j]){matrix[i][0] = 0;matrix[0][j] = 0;}}}for(int i=1; i<m; ++i){for(int j=1; j<n; ++j){if(!matrix[i][0] || !matrix[0][j]) {matrix[i][j] = 0;}}}//处理第零行、第零列元素if(flag_col0){for(int i=0; i<m; ++i){matrix[i][0] = 0;}}if(flag_row0){for(int j=0; j<n; ++j){matrix[0][j] = 0;}}}
};

02 螺旋矩阵

class Solution {
public:vector<int> spiralOrder(vector<vector<int>>& matrix) {}
};
方法一:模拟

时间复杂度 O(mn),空间复杂度 O(mn)

  • 建立二维数组 visited(rows, vector<bool>(columns),存储矩阵元素的访问情况

  • 遍历矩阵,判断 nextRownextColumn 是否越过边界

class Solution {
public:static constexpr int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; //向右、下、左、上vector<int> spiralOrder(vector<vector<int>>& matrix) {if (matrix.size() == 0 || matrix[0].size() == 0) return {};int rows = matrix.size(), columns = matrix[0].size(), total = rows * columns;vector<vector<bool>> visited(rows, vector<bool>(columns)); //标记vector<int> order(total); //答案int row = 0, column = 0, dirIndex = 0;for(int i=0; i<total; i++){order[i] = matrix[row][column]; //更新答案visited[row][column] = true; //更新标记int nextRow = row + dirs[dirIndex][0];int nextColumn = column + dirs[dirIndex][1];if(nextRow >= rows || nextRow < 0 || nextColumn >= columns || nextColumn < 0 || visited[nextRow][nextColumn]){dirIndex = (dirIndex + 1) % 4;}row += dirs[dirIndex][0];column += dirs[dirIndex][1];}return order;}
};

static 意味着变量在程序的生命周期内只会被分配一次,并且在所有对该数组的函数调用中共享同一存储空间。

constexpr 用于指定变量值在编译时就确定下来,它提示编译器尽量进行优化。

{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}每一对元素代表在二维平面上一个方向的坐标变换:

  • {0, 1}:代表向右移动 —— 行不变,列加一

  • {1, 0}:代表向下移动 —— 行加一,列不变

  • {0, -1}:代表向左移动 —— 行不变,列减一

  • {-1, 0}:代表向上移动 —— 行减一,列不变

vector<bool>(columns) 创建一个布尔向量,大小为 columns,其中每个元素默认初始化为 false

vector<vector<bool>>(rows, ...) 表示创建一个这样的布尔向量的向量,其长度为 rows,即每一行都是一个布尔向量,且每列都是初始化为 false

spiral 螺旋形的 constexpr 常量表达式

⑦ 在什么样的情况下 if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]) 中的 nextRow < 0 成立?

由于初始位置是 (0, 0) 且遍历顺序是顺时针螺旋,因此 nextRow < 0 通常在当前方向反转尝试向上之前使用过的路径上被访问时发生。

⑧ 将代码中涉及 nextRownextColumn 部分的片段改为如下片段如何?

if((column == (columns - 1)) || (row == (rows - 1)) || (column == 0) || matrix[row + dirs[dirIndex][0]][column + dirs[dirIndex][1]]){dirIndex = (dirIndex + 1) % 4;
}

 matrix[row + dirs[dirIndex][0]][column + dirs[dirIndex][1]]:这种方式不仅增加了代码复杂度,并且可能由于超出 matrix 的边界而导致访问无效内存,出现内存错误。

if (column == columns || row == rows || column == -1 || row == -1 ||
row + dirs[dirIndex][0] < 0 || row + dirs[dirIndex][0] >= rows ||
column + dirs[dirIndex][1] < 0 || column + dirs[dirIndex][1] >= columns || 
visited[row + dirs[dirIndex][0]][column + dirs[dirIndex][1]]) {
dirIndex = (dirIndex + 1) % 4;
}

row + dirs[dirIndex][0] < 0:检查向当前方向移动后,新的行索引是否小于0。这会保持我们不越过上边界。

row + dirs[dirIndex][0] >= rows:检查向当前方向移动后,新的行索引是否大于或等于总行数。这会确保我们不越过下边界。

column + dirs[dirIndex][1] < 0:检查向当前方向移动后,新的列索引是否小于0。这会确保我们不越过左边界。

column + dirs[dirIndex][1] >= columns:检查向当前方向移动后,新的列索引是否大于或等于总列数。这会确保我们不越过右边界。

方法二:按层模拟

时间复杂度 O(mn),空间复杂度 O(1)

class Solution {
public:vector<int> spiralOrder(vector<vector<int>>& matrix) {if(matrix.size() == 0 || matrix[0].size() == 0) return {};vector<int> order;int rows = matrix.size(), columns = matrix[0].size();int left = 0, right = columns-1, top = 0, bottom = rows-1;//主打一个遍历while(left<=right && top<=bottom){for(int column=left; column<=right; ++column) {order.push_back(matrix[top][column]);}for(int row=top+1; row<=bottom; ++row) {order.push_back(matrix[row][right]);}if(left<right && top<bottom){for(int column=right-1; column>=left+1; --column) {order.push_back(matrix[bottom][column]);}for(int row=bottom; row>=top+1; --row) {order.push_back(matrix[row][left]);}}top++;left++;right--;bottom--;}return order;}
};

① 为什么还要第二次判断 if(left<right && top<bottom) 呢?

在只有一行或者一列剩下时,第二次顺时针迭代会导致重复元素被添加到结果中。例如,当只剩下一行时,上面的第二次和第三次迭代(从右向左)会和已经处理的行产生重复。比如:

  • 单行(例如,[[1, 2, 3]]

  • 单列(例如,[[1], [2], [3]]

03 旋转图像

class Solution {
public:void rotate(vector<vector<int>>& matrix) {}
};
方法一:辅助数组

时间复杂度 O(n^2),空间复杂度 O(n^2)

class Solution {
public:void rotate(vector<vector<int>>& matrix) {auto matrix_new = matrix;int n = matrix.size();for(int i=0; i<n; ++i){for(int j=0, j<n; ++j){matrix_new[j][n-1-i] = matrix[i][j];}}return matrix_new;}
};

auto matrix_new = matrix;这行代码的作用是复制matrix变量的值到一个新的变量matrix_new中,matrix_new的类型与matrix一致。

对于大多数与标准库相关的容器(如 std::vector),这会创建 matrix 的一个浅拷贝,整体上是深拷贝其内容,而不是仅仅复制指针(如果它是一个复杂数据结构)。

方法二:原地旋转

时间复杂度 O(n^2),空间复杂度 O(1)

class Solution {
public:void rotate(vector<vector<int>>& matrix) {int n = matrix.size();for(int x=0; x<n/2; ++x){for(int y=0; y<(n+1)/2; ++y){int flag = matrix[x][y];matrix[x][y] = matrix[n-1-y][x];matrix[n-1-y][x] = matrix[n-1-x][n-1-y];matrix[n-1-x][n-1-y] = matrix[y][n-1-x];matrix[y][n-1-x] = flag;}}}
};
方法三:用翻转代替旋转

时间复杂度 O(n^2),空间复杂度 O(1)

class Solution {
public:void rotate(vector<vector<int>>& matrix) {int n = matrix.size();for(int x=0; x<n/2; ++x){for(int y=0; y<n; ++y){swap(matrix[x][y], matrix[n-1-x][y]);}}for(int x=0; x<n; ++x){for(int y=0; y<x; ++y){swap(matrix[x][y], matrix[y][x]);}}}
};

04 搜索二维矩阵 Ⅱ

class Solution {
public:bool searchMatrix(vector<vector<int>>& matrix, int target) {}
};
方法一:直接查找

时间复杂度 O(mn),空间复杂度 O(1)

class Solution {
public:bool searchMatrix(vector<vector<int>>& matrix, int target) {for(const auto& row: matrix){for(int element: row){if(element == target) return true;}}return false;}
};
方法二:二分法

时间复杂度 O(mlogn),空间复杂度 O(1)

class Solution {
public:bool searchMatrix(vector<vector<int>>& matrix, int target) {for(const auto& row: matrix){auto it = lower_bound(row.begin(), row.end(), target);if(it != row.end() && *it == target) return true;}return false;}
};

lower_bound 是一个标准库函数,位于 <algorithm> 头文件中,用于在一个已排序的范围内查找目标值的位置。lower_bound 返回一个迭代器,指向范围内第一个不小于目标值的元素的位置,如果所有的元素都小于目标值,它将返回指向末尾的迭代器。

注意:使用 lower_bound  的前提是 row 必须是排序好的,否则结果是不确定的。

方法三:Z字形查找
class Solution {
public:bool searchMatrix(vector<vector<int>>& matrix, int target) {int m = matrix.size(), n = matrix[0].size();int x = 0, y = n-1;while(x < m && y >=0){if(matrix[x][y] == target) return true;else if(matrix[x][y] > target) y--;else x++;}return false;}
};

文章部分代码来源于力扣(LeetCode)

http://www.jinmujx.cn/news/78389.html

相关文章:

  • 网站页面总数百度一下生活更好
  • 做轻时尚的网站云巅seo
  • 做百度网站接到多少客户电话号码免费注册网页网址
  • 自适应网站 seo怎么做北京优化seo排名优化
  • 我做外贸要开国际网站吗怎样推广自己的商城
  • 个体营业执照可以做网站嘛网络营销渠道的功能
  • 温州微网站制作多少钱百度域名收录
  • seo优化排名怎么做快速seo优化
  • 自己做好的网站如何发布百度seo怎么做网站内容优化
  • 网站的空间什么意思seo搜索引擎入门教程
  • wordpress仿微博主题长春seo
  • dw网站指向邮箱超链接怎么做seo的工作内容
  • 政府网站建设征求意见问卷电商网站设计
  • 做网站分销违法吗新闻摘抄
  • 收银系统哪个软件好seo经理
  • 网站优化布局做营销策划的公司
  • 餐饮网站界面机构类网站有哪些
  • 定制类网站建设网站建设开发
  • 房产经济人怎么做网站吉林关键词排名优化软件
  • 为什么选php语言做网站千锋教育介绍
  • 合肥网站优化公司网站运营一个月多少钱
  • 南通公司网站建设东莞网站制作模板
  • 浏览器禁止网站怎么做百度最贵关键词排名
  • 网站开发有关书籍网址链接
  • seo网站优化公司海外广告投放公司
  • 建个商城网站多少钱万能搜索网站
  • 一个空间做两个网站的视频教程网页在线代理翻墙
  • 腾讯云怎么备案网站吗百姓网推广怎么收费标准
  • 做个英文网站网站免费制作平台
  • 西安网站建设网站推广市场营销手段有哪四种