Skip to main content
  1. Posts/

leetcode 54. Spiral Matrix (矩阵蛇形取数)

·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

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

思路:。。。再次让我回想起高一的暑假。。。。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月11日 星期二 19时42分05秒
 4File Name :54.cpp
 5************************************************ */
 6class Solution {
 7public:
 8    int n,m; //0右,1下,2左,3上
 9    int cal( int &x,int &y,int dir,vector<vector<bool> > & vis)
10    {
11	if (dir==0)
12	{
13	    if (y<=n-2&&!vis[x][y+1]) y++;
14	    else
15	    {
16		dir++;
17		x++;
18	    }
19	    return dir;
20	}
21	if (dir==1)
22	{
23	    if (x<=m-2&&!vis[x+1][y]) x++;
24	    else
25	    {
26		dir++;
27		y--;
28	    }
29	    return dir;
30	}
31	if (dir==2)
32	{
33	    if (y>=1&&!vis[x][y-1]) y--;
34	    else
35	    {
36		dir++;
37		x--;
38	    }
39	    return dir;
40	}
41	if (dir==3)
42	{
43	    if (x>=1&&!vis[x-1][y]) x--;
44	    else
45	    {
46		dir = 0 ;
47		y++;
48	    }
49	    return dir;
50	}
51    }
52    vector<int> spiralOrder(vector<vector<int>>& matrix) {
53	vector<int>res;
54	m = matrix.size();
55	if (m==0) return res;
56	n = matrix[0].size();
57	if (n==0) return res;
58	vector<vector<bool> >vis(m,vector<bool>(n,false));
59	int x,y,dir;
60	x = y = dir = 0 ;
61	for ( int i = 0 ; i < n*m ;  i++)
62	{
63	    printf("x:%d y:%d\n",x,y);
64	    res.push_back(matrix[x][y]);
65	    vis[x][y] = true;
66	    dir = cal(x,y,dir,vis);
67	}
68	return res;
69    }
70};

Related

leetcode 55. Jump Game (dp)

·1 min
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 思路:dp[i]表示能否到达位置i…无脑dp即可。。。

leetcode 56. Merge Intervals (模拟,求相交区间)

·1 min
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 思路:扫一遍即可。。 1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月11日 星期二 19时15分30秒 4File Name :56.cpp 5************************************************ */ 6/** 7 8 * Definition for an interval. 9 10 * struct Interval { 11 12 * int start; 13 14 * int end; 15 16 * Interval() : start(0), end(0) {} 17 18 * Interval(int s, int e) : start(s), end(e) {} 19 20 * }; 21 22 */ 23 24class Solution { 25 26public: 27 28 int n; 29 static bool cmp(Interval A,Interval B) 30 { 31 return A.start<B.start; 32 } 33 vector<Interval> merge(vector<Interval>& pi) { 34 vector<Interval>res; 35 n = pi.size(); 36 if (n==0) return res; 37 sort(pi.begin(),pi.end(),cmp); 38 int l = -1,r = -1; 39 for ( int i = 0 ; i < n ; i++) 40 { 41 if (l==-1&&r==-1) 42 { 43 l = pi[0].start; 44 r = pi[0].end; 45 continue; 46 } 47 if (pi[i].start<=r) 48 { 49 r = max(r,pi[i].end); 50 continue; 51 } 52 if (pi[i].start>r) 53 { 54 res.push_back(Interval(l,r)); 55 l = pi[i].start; 56 r = pi[i].end; 57 continue; 58 } 59 } 60 //最后一组不要忘记 61 res.push_back(Interval(l,r)); 62 int siz = res.size(); 63 for ( int i = 0 ; i < siz ;i++) printf("%d ",res[i].start,res[i].end); 64 65 66 return res; 67 68 } 69 70};

leetocde 63. Unique Paths II

·1 min
Follow up for “Unique Paths”: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid.

leetcode 64. Minimum Path Sum (二维dp)

·1 min
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time.