开宗明义:本系列基于牛客网剑指offer,刷题小白,一天两道我快乐!旨在理解和交流,重在记录,望各位大牛指点!
牛客网-剑指offer
文章目录
1、顺时针打印数组
描述:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,如输入一个 4*4 矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.。
思路1:用左上和右下的坐标定位出一次一圈要旋转打印的数据,一次旋转打印一圈结束后,往对角分别前进和后退一个单位,后两个for循环,加入条件判断,是为了防止出现单行或者单列的情况。。
测试代码:
#include <vector>
#include <stdio.h>
using namespace std;
class Solution {
public:
vector<int> printMatrix(vector<vector<int>> matrix) {
int row = matrix.size(); //行
int col = matrix[0].size(); //列
vector<int> res;
//输入的数组尾非法数组,返回空
if (row == 0 || col == 0) {
return res;
}
//定义四个关键变量,表示左上和右下的打印范围
int left = 0;
int top = 0; //左上
int right = col - 1; //右下
int bottom = row - 1;
while (left <= right && top <= bottom) {
// left to right
for (int i = left; i < right; ++i) {
res.push_back(matrix[top][i]);
}
// top to bottpm
for (int i = top + 1; i <= bottom; ++i) {
res.push_back(matrix[i][right]);
}
// right to left
if (top != bottom) {
for (int i = right - 1; i >= left; --i) {
res.push_back(matrix[bottom][i]);
}
}
//bottom to top
if (left != right) {
for (int i = bottom - 1; i > top; --i) {
res.push_back(matrix[i][left]);
}
}
//
left++;
top++;
right--;
bottom--;
}
return res;
}
};
思路2:顺时针打印就是按圈数循环打印,一圈包含两行或者两列,在打印的时候会出现某一圈中只包含一行,要判断从左向右打印和从右向左打印的时候是否会出现重复打印,同样只包含一列时,要判断从上向下打印和从下向上打印的时候是否会出现重复打印的情况,本质和思路1一样的。
测试代码:
#include <vector>
#include <stdio.h>
using namespace std;
class Solution {
vector<int> printMatrix(vector<vector<int>> matrix) {
vector<int> res;
res.clear();
//
int row = matrix.size(); //行数
int collor = matrix[0].size(); //列数
//计算打印的圈数
int cricle = ((row < collor ? row : collor) - 1) / 2 + 1;
//
for (int i = 0; i < cricle; i++) {
//从左向右打印
for (int j = i; j < collor - i; j++) {
res.push_back(matrix[i][j]);
}
//从上往下的每一列数据
for (int k = i + 1; k < row - i; k++) {
res.push_back(matrix[k][collor - 1 - i]);
}
//帕努单是否会重复打印(从右向左的每行数据)
for (int m = collor - i - 2; (m >= i) && (row - i - 1 != i); m--) {
res.push_back(matrix[row - i - 1][m]);
}
//判断是否重复打印(从下往上的每一列数据)
for (int n = row - i - 2; (n > i) && (collor - i - 1 != i); n--) {
res.push_back(matrix[n][i]);
}
}
return res;
}
};
2、包含min函数的栈
描述:定义栈的数据结构,在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度为O(1))。
思路:应用一个辅助栈;
测试代码:
#include <stack>
#include <stdio.h>
using namespace std;
class Solution {
public:
void push(int value) {
StackInt.push(value); //首先将 value push进入StackInt这个中转栈
//
if (StackMin.empty()) { //如果最小栈未空,则push进去
StackMin.push(value);
}
else { //如果最小栈不为空
if (StackMin.top() < value) { //比较top()与value的大小
StackMin.push(StackMin.top());
}
else {
StackMin.push(value);
}
}
}
//
void pop(){
if (!StackInt.empty()) {
StackInt.pop();
StackMin.pop();
}
}
//
int top() {
return StackInt.top();
}
//
int min() {
return StackMin.top();
}
//
private:
stack<int> StackInt; //中转栈
stack<int> StackMin; //最小栈
};
或者
class Solution {
public:
stack<int> stack1,stack2;
void push(int value) {
stack1.push(value);
if(stack2.empty())
stack2.push(value);
else
if(value<=stack2.top()){
stack2.push(value);
}
}
void pop() {
if(stack1.top()==stack2.top())
stack2.pop();
stack1.pop();
}
int top() {
return stack1.top();
}
int min() {
return stack2.top();
}
};