【LeetCode】Search a 2D Matrix & Set Matrix Zeroes & Spiral Matrix & Spiral Matrix II
2014-04-22 16:02
543 查看
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++标准函数库http://download.csdn.net/detail/chinasnowwolf/7108919
在线C++API查询
http://www.cplusplus.com/
vector使用方法
http://www.cplusplus.com/reference/vector/vector/
相关文章推荐
- [LeetCode] Spiral Matrix、Rotate Image、Spiral Matrix II、Set Matrix Zeroes、Search a 2D Matrix
- Intersection of Two Linked Lists,Search a 2D Matrix,Set Matrix Zeroes,Combinations,Spiral Matrix
- leetcode题解-74. Search a 2D Matrix && 240. Search a 2D Matrix II
- Leetcode 74. Search a 2D Matrix & 240. Search a 2D Matrix II
- LeetCode -- Search a 2D Matrix & Search a 2D Matrix II
- Search a 2D Matrix & Set Matrix Zeroes & Combinations
- LeetCode - 74. Search a 2D Matrix & 240. Search a 2D Matrix II
- <LeetCode OJ> 73. Set Matrix Zeroes
- 74.Search a 2D Matrix&240. Search a 2D Matrix II
- 74. Search a 2D Matrix&240. Search a 2D Matrix II
- LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
- LeetCode-74&240.Search a 2D Matrix
- 【LeetCode从零单刷】Search a 2D Matrix I & II
- <LeetCode OJ> 74. / 240. Search a 2D Matrix (I / II)
- !leetcode[74&240]:Search a 2D Matrix I &II
- leetcode题解-73. Set Matrix Zeroes && 74. Search a 2D Matrix
- 【LeetCode】Search a 2D Matrix && 【九度】题目1384:二维数组中的查找
- LeetCode | 74. Search a 2D Matrix
- [LeetCode]Search a 2D Matrix
- [leetcode] Set Matrix Zeroes