Leetcode题解(十九)

54、Spiral Matrix

题目:

题目意思很简单,就是螺旋式访问矩阵元素。也没有比较经典的算法可以解决此题,只需要模拟一下这个过程即可。

代码如下:

 1 class Solution {
 2 public:
 3     vector<int> spiralOrder(vector<vector<int>>& matrix) {
 4         vector<int> res;
 5         if(matrix.empty())
 6             return res;
 7         int m = matrix.size();
 8         int n = matrix[0].size();
 9     
10         
11         int i,j;
12         
13         bool **arr = new bool*[m];
14         for(i=0;i<m;i++)
15         {
16             arr[i] = new bool[n];
17             memset(arr[i],0,sizeof(bool));
18         }
19         
20         int flag=0;//0表示向右,1表示向下,2表示向左,3表示向上
21         
22         int count = 0;
23         i=0;
24         j=0;
25         while(true)
26         {
27             res.push_back(matrix[i][j]);
28             arr[i][j] = true;
29             count++;
30             if(count == m*n)
31                 break;
32             
33             
34             if(0 == flag)
35             {
36                 j++;                    
37             }                
38             else if(1 == flag)
39                 i++;
40             else if(2 == flag)
41                 j--;
42             else
43                 i--;
44             
45             if(i<0 || i>=m || j<0 || j>=n || arr[i][j])//越界了或者访问到已经被访问过的元素
46             {
47                 
48                 if(i<0)
49                     i=0;
50                 if(j<0)
51                     j=0;
52                 if(i>=m)
53                     i=m-1;
54                 if(j>=n)
55                     j=n-1;
56                 if(arr[i][j])
57                 {
58                     if(0 == flag)
59                         j--;
60                         
61                     else if(1 == flag)
62                         i--;
63                     else if(2 == flag)
64                         j++;
65                     else
66                         i++;
67                 }
68                 
69                 flag = (flag + 1)%4;
70                 res.pop_back();
71                 count--;
72             }        
73                 
74         }
75         return res;
76         
77     }
78 };

 -----------------------------------------------------------------------分割线-----------------------------------------------------------------------

55、Jump Game

题目:

分析:针对数组中每一个元素,计算其最大能跳多远,并将这个最远值记录好。

代码如下:

 1 class Solution {
 2 public:
 3     bool canJump(vector<int>& nums) {
 4         const int size = nums.size();
 5         int index = 0;
 6         int max = 0;
 7         for(;index<=size-1-1;index++)//注意等号,
 8         {
 9             if(index + nums[index] > max)
10                 max = index + nums[index];
11             if(index >= max)//针对用例[0,1,2]
12                 return false;
13         }
14         if(max >= size-1)
15             return true;
16         else
17             return false;
18     }
19 };

 -------------------------------------------------------------------------------分割线-------------------------------------------------------------------

58、Length of Last Word

题目

题目很简单,直接贴代码

 1 class Solution {
 2 public:
 3     int lengthOfLastWord(const char *s) {
 4         int len=strlen(s);
 5         int sum=0;
 6         while(s[len-1]==' ') len--;
 7         for(int i=len-1;i>=0;i--)
 8         {
 9             if(s[i]!=' ')   sum++;
10             else break;
11         }
12         return sum;
13     }
14 };
原文地址:https://www.cnblogs.com/LCCRNblog/p/5171108.html