【LeetCode】Search a 2D Matrix & Set Matrix Zeroes & Spiral Matrix & Spiral Matrix II

Search a 2D Matrix

 

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

For example,

Consider the following matrix:

[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]

Given target = 3, return true.


class Solution {
public:
    bool searchMatrix(vector<vector<int> > &matrix, int target) {
        int m = matrix.size();
        if(m < 1)	return false;
        int n = matrix[0].size();
        int low = 0, high = m*n-1;
        while(low<=high)
        {
        	int mid = (low+high)/2;
        	int row = mid/n;
        	int col = mid%n;
        	if(target == matrix[row][col])	return true;
        	if(target > matrix[row][col])	low = mid + 1;
        	else 							high = mid - 1;
		}
		return false;
    }
};



Set Matrix Zeroes

 

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

click to show follow up.

Follow up:

Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?




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



Spiral Matrix

 

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

For example,
Given the following matrix:

[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]

You should return [1,2,3,6,9,8,7,4,5].


class Solution {
public:
    vector<int> spiralOrder(vector<vector<int> > &matrix) {
        vector<int> ret;
        if(matrix.size()<1)	return ret;
        for(int i = 0;i<(min(matrix.size(),matrix[0].size())+1)/2;i++)
        	spiralOrder(ret,matrix,i);
        return ret;
    }
    void spiralOrder(vector<int> &ret,vector<vector<int> > &matrix,int start)
    {//左上到右上 
    	int m =  matrix.size(), n = matrix[0].size();
    	for(int i = start;i<n-start;i++)
    		ret.push_back(matrix[start][i]);
   		if(m - start*2 == 1) return;/!!!!!!
   	//右上到右下	
   		for(int i = start+1;i<m-start;i++)
    		ret.push_back(matrix[i][n-start-1]);
   		if(n - start*2 == 1) return;!!!!!!
	//右下到左下
        for(int i = n-start-2;i>= start;i--)
    		ret.push_back(matrix[m-start-1][i]);
	//右下到左上
		for(int i = m-start-2;i> start;i--)
    		ret.push_back(matrix[i][start]);
	}
};


Spiral Matrix II

 

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

For example,
Given n = 3,

You should return the following matrix:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]

class Solution {
public:
    vector<vector<int> > generateMatrix(int n) {
        vector<vector<int> > ret(n,vector<int> (n));
        int sum = 1;
        for(int i = 0;i<(n+1)/2;i++)
        	generateMatrixCore(ret,sum,i);
       	return ret;
    }
    void generateMatrixCore(vector<vector<int> > &ret,int &sum,int start)
    {//左上到右上 
    	int n = ret.size();
    	for(int i = start;i<n-start;i++)
    		ret[start][i] = sum++;
   		if(n - start*2 == 1) return;
   	//右上到右下	
   		for(int i = start+1;i<n-start;i++)
    		ret[i][n-start-1] = sum++;
	//右下到左下
        for(int i = n-start-2;i>= start;i--)
    		ret[n-start-1][i] = sum++;
	//右下到左上
		for(int i = n-start-2;i> start;i--)
    		ret[i][start] = sum++;
	}
};






推荐学习C++的资料

C++标准函数库
在线C++API查询
vector使用方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值