↓ 跳过正文
  1. Posts/

leetocde 59. Spiral Matrix II (模拟)

·274 字·1 分钟

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

思路:仿佛回到高一的那个暑假。。。

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

相关文章

leetocde 63. Unique Paths II

·337 字·1 分钟
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. For example, There is one obstacle in the middle of a 3x3 grid as illustrated below. 1[ 2 [0,0,0], 3 [0,1,0], 4 [0,0,0] 5] The total number of unique paths is 2. 题意:从左上到右下的方案数,有些点不能走。

leetcode 64. Minimum Path Sum (二维dp)

·371 字·1 分钟
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. 数字三角形。。。。从左上到右下问最短路径。。每次只能向下或者向右。。。 wa了一次。。。是因为边界值赋值成了0.。。求最短路径显然因为赋值成inf才对orz..果然傻了。。

leetcode 73. Set Matrix Zeroes (矩阵置0,乱搞)

·926 字·2 分钟
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(m__n) 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? 直接放常数空间的做法。 这道题面hypereal的时候遇到过,基本思路就是用已经确定是0的位置来存储其他行和列的信息。

leetcode 238. Product of Array Except Self (乱搞)

·411 字·1 分钟
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2,3,4], return [24,12,8,6]. Follow up: Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.) 先来个O(n)空间的无脑解法。。。一个前缀积一个后缀积就好了。。。

leetcode 79. Word Search (dfs)

·374 字·1 分钟
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. 思路:dfs即可。记得要回溯一下… 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月07日 星期五 14时32分54秒 4File Name :79.cpp 5************************************************ */ 6class Solution { 7 8public: 9 10 int n,m; 11 const int dx4[4]={1,-1,0,0}; 12 const int dy4[4]={0,0,-1,1}; 13 bool vis[1005][1005]; 14 int len; 15 16 bool yes( int x,int y) 17 { 18 if (x>=0&&x<=n-1&&y>=0&&y<=m-1) return true; 19 return false; 20 } 21 22 bool dfs( int x,int y,int cur,vector<vector<char> > & maze,string & st) 23 { 24// printf("x:%d y:%d cur : %d\n",x,y,cur); 25 if (cur>=len) return true; 26 for ( int i = 0 ; i < 4 ; i++) 27 { 28 int nx = x + dx4[i]; 29 int ny = y + dy4[i]; 30 if (!yes(nx,ny)) continue; 31 if (vis[nx][ny]) continue; 32 if (maze[nx][ny]!=st[cur]) continue; 33 vis[nx][ny] = true; 34 bool res = dfs(nx,ny,cur+1,maze,st); 35 if (res) return true; 36 vis[nx][ny] = false ;//好像还要回溯一下啊? 37 } 38 return false; 39 } 40 41 bool exist(vector<vector<char>>& board, string word) { 42 n = board.size(); 43 if (n==0) return false; 44 m = board[0].size(); 45 if (m==0) return false; 46 len = word.length(); 47 cout<<"len:"<<len<<endl; 48 char beg = word[0]; 49 for ( int i = 0 ; i < n ; i++) 50 for ( int j = 0 ; j < m ; j++) 51 if (board[i][j]==beg) 52 { 53 memset(vis,false,sizeof(vis)); 54 //起点忘记打标记了。。。智力-2.。。 55 vis[i][j] = true; 56 bool ok = dfs(i,j,1,board,word); 57 cout<<"ok:"<<ok<<endl; 58 if (ok) return true; 59 } 60 return false; 61 62 63 } 64 65};