题解 | #二维数组操作#

二维数组操作

http://www.nowcoder.com/practice/2f8c17bec47e416897ce4b9aa560b7f4

题目的主要信息:

建立一个表格,对表格进行交换元素、插入行、插入列、查找值的操作,如果操作能成功执行则返回0;如果操作失败则返回-1。导致操作失败的原因有超过表格范围、超过最大规格大小即各种越界问题。

方法一:

对输入的数据进行判断

  1. m和n如果小于等于9,则操作成功;否则操作失败,并将大写恢复到最大规格9x9范围内。
  2. 交换(x1,y1)(x2,y2)的值。如果x1,y1,x2,y2均在(m,n)范围内则操作成功。
  3. 在x3的上面添加一行,x3需要在表格范围内,且表格的行数小于9。
  4. 在y3的左边添加一列,y3需在表格范围内,且表格的列数小于9。
  5. 查找(x4,y4)的值,这个坐标不能超过表格的范围,即不越界则操作成功。 alt

具体做法:

#include<iostream>

using namespace std;

int main(){
    int m, n;
    int x1, y1, x2, y2, x3, y3, x4, y4;//所有输入
    while(cin >> m >> n >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4){
        if(m <= 9 && n <= 9){//表格的最大规格为9x9
            cout << 0 << endl;
        }else{
            cout << -1 << endl;
            if(m > 9) m = 9;//如果超出了应该恢复到范围内
            if(n > 9) n = 9;
        }
        if(x1 < m && y1 < n && x2 < m && y2 < n){//交换(x1,y1)(x2,y2),判断他们是否在表格范围内
            cout << 0 << endl;
        }else{
            cout << -1 << endl;
        }
        if(x3 < m && m < 9){//在x3的上面添加一行,x3需在表格范围内,且表格的行数小于9行
            cout << 0 << endl;
        }else{
            cout << -1 << endl;
        }
        if(y3 < n && n < 9){//在y3的左边添加一列,y3需在表格范围内,且表格的列数小于9列
            cout << 0 << endl;
        }else{
            cout << -1 << endl;
        }
        if(x4 < m && y4 < n){//查找(x4,y4)的值,坐标需在表格范围内
            cout << 0 << endl;
        }
        else{
            cout << -1 << endl;
        }
    }
}

复杂度分析:

  • 时间复杂度:O(1)O(1),只进行判断,没有循环。
  • 空间复杂度:O(1)O(1),只用了常数空间。

方法二:

方法一没有对二维数组的实际操作,在这里构建一个Chart类,将初始化建表、交换坐标值、插入行、插入列、查找坐标值作为成员函数,在二维数组data上进行操作。每一个成员函数的返回值是0或者-1,表示该操作成功或失败。

具体做法:

#include<iostream>
#include<vector>

using namespace std;

class Chart{
    public:
    int init(int m, int n);//初始化表格
	int swap(int x1, int y1, int x2, int y2);//交换坐标值
	int insert_row(int x);//插入行
    int insert_column(int y);//插入列
	int query(int x, int y);//查询
    
    private:
    int m;
	int n;
	vector<vector<int>> data;//二维数组
};

int Chart::init(int m, int n)//初始化表格大小为mxn
{
	if (m > 0 && m <= 9 && n > 0 && n <= 9) {//m和n的值如果合法
		this->m = m;
		this->n = n;
		data.resize(m);
		for (int i = 0; i < m; i++) {
			data[i].resize(n);
		}
		return 0;
	}
	else {
		return -1;
	}
}

int Chart::swap(int x1, int y1, int x2, int y2)//交换坐标值
{
	if (x1 >= 0 && x1 < m && x2 >= 0 && x2 < m && y1 >= 0 && y1 < n && y2 >= 0 && y2 < n) {//坐标的范围要合法
		int temp = data[x1][y1];
		data[x1][y1] = data[x2][y2];
		data[x2][y2] = temp;
		return 0;
	}
	else {
		return -1;
	}
}

int Chart::insert_row(int x)//插入行
{
    if (x >= 0 && x < m && m < 9) {//x在表格范围内
			return 0;
		}
		else {
			return -1;
		}
}
int Chart::insert_column(int y)//插入列
{
    if (y >= 0 && y < n && n < 9) {//y在表格范围内
			return 0;
		}
		else {
			return -1;
		}
}
int Chart::query(int x, int y)//查询坐标上的值
{
	if (x >= 0 && x < m && y >= 0 && y < n) {//坐标不越界
		return 0;
	}
	else {
		return -1;
	}
}

int main()
{
	int m, n, x1, y1, x2, y2, x3, y3, x4, y4;
	Chart chart;
	while (cin >> m >> n >>  x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4){
		cout << chart.init(m, n) << endl;
		cout << chart.swap(x1, y1, x2, y2) << endl;
		cout << chart.insert_row(x3) << endl;
		cout << chart.insert_column(y3) << endl;
		cout << chart.query(x4, y4) << endl;
	}
	return 0;
}

复杂度分析:

  • 时间复杂度:O(1)O(1),没有循环。
  • 空间复杂度:O(mn)O(mn),构建了mnm*n大小的表格。
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务