码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode-955 Delete Columns to Make Sorted II(删列造序 ||)

时间:2018-12-09 16:21:45      阅读:502      评论:0      收藏:0      [点我收藏+]

标签:empty   cto   ==   deletion   als   false   erase   color   tco   

 1 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 2 
 3 
 4 string ooder = "abcdefghijklmnopqrstuvwxyz";
 5 bool cmp(const string &a,const string &b)
 6 {
 7     for(int i = 0; i < min(a.size(),b.size()); i ++)
 8     {
 9         if(a[i]!=b[i])
10         {
11             for(int j = 0; j < ooder.size(); j ++)
12             {
13                 if(ooder[j]==a[i])
14                     return 1;
15                 else if(ooder[j]==b[i])
16                     return 0;
17             }
18         }
19     }
20     return true;
21 }
22 
23 bool ok(vector<string> A)
24 {
25     for(int i = 0; i < A.size()-1; i++)
26     {
27         if(!cmp(A[i],A[i+1]))
28             return false;    
29     }
30     return true;
31 }
32 
33 class Solution
34 {
35     public:
36         int minDeletionSize(vector<string>& A)
37         {
38             if(ok(A))
39                 return 0;
40             int rnt = 0;
41             int end = 0;
42             int flag = 0;
43             vector<string> tmp = A;
44             while(!tmp[0].empty()&&!ok(tmp))
45             {
46                 for(int i = 0; i < tmp.size()-1; i ++)
47                 {
48                     if(tmp[i][end]>tmp[i+1][end])
49                     {
50                         if(end!=0&&tmp[i][end-1]<tmp[i+1][end-1])
51                             continue;
52                         flag = 1;
53                         break;
54                     }
55                 }
56                 if(flag)
57                 {
58                     _for(k,0,tmp.size())
59                         tmp[k].erase(end, 1);
60                     rnt ++;
61                     end --;
62                 }
63                 flag = 0;
64                 end ++;
65             }
66             return rnt;
67         }
68 };

 

Leetcode-955 Delete Columns to Make Sorted II(删列造序 ||)

标签:empty   cto   ==   deletion   als   false   erase   color   tco   

原文地址:https://www.cnblogs.com/Asurudo/p/10090765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!