1. 程式人生 > >Leetcode-942 DI String Match(增減字串匹配)

Leetcode-942 DI String Match(增減字串匹配)

 1 class Solution
 2 {
 3     public:
 4         vector<int> diStringMatch(string S)
 5         {
 6             vector<int> tmp {0};
 7             for(auto c:S)
 8             {
 9                 int a = tmp.back();
10                 if(c=='I')
11                 {
12                     tmp.push_back(a+1
); 13 } 14 else 15 { 16 tmp.push_back(a-1); 17 } 18 } 19 // for(auto d:tmp) 20 // cout << d << endl; 21 int max = S.size(); 22 vector<int
> result; 23 for(int i = 0;i < max+1;i ++) 24 result.push_back(0); 25 for(int i = 1000;i >= -1000;i --) 26 { 27 for(int j = 0;j < tmp.size();j ++) 28 { 29 if(tmp[j]==i) 30 {
31 result[j] = max --; 32 } 33 } 34 } 35 return result; 36 } 37 };