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

 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 };

 

转载于:https://www.cnblogs.com/Asurudo/p/10090765.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值