标签:code 字符串 col pre size lips tco vector end
1 class Solution 2 { 3 public: 4 int minFlipsMonoIncr(string S) 5 { 6 vector<int> left2CountOne (S.size(),0); 7 vector<int> right2CountZero (S.size(),0); 8 9 for(int i = 0;i < S.size();i ++) 10 { 11 if(S[i]==‘1‘) 12 { 13 if(i==0) 14 left2CountOne[i] = 1; 15 else 16 left2CountOne[i] = left2CountOne[i-1] + 1; 17 } 18 else if(S[i]==‘0‘ && i!=0) 19 left2CountOne[i] = left2CountOne[i-1]; 20 } 21 22 for(int i = S.size()-1;i >= 0 ;i --) 23 { 24 if(S[i]==‘0‘) 25 { 26 if(i==S.size()-1) 27 right2CountZero[i] = 1; 28 else 29 right2CountZero[i] = right2CountZero[i+1] + 1; 30 } 31 else if(S[i]==‘1‘ && i != S.size()-1) 32 right2CountZero[i] = right2CountZero[i+1]; 33 } 34 35 int result = right2CountZero[0]; 36 for(int i = 0;i < S.size()-1;i ++) 37 { 38 if(left2CountOne[i]+right2CountZero[i+1] < result) 39 result = left2CountOne[i]+right2CountZero[i+1]; 40 } 41 if(result > left2CountOne[S.size()-1]) 42 result = left2CountOne[S.size()-1]; 43 // cout << left2CountOne[S.size()-1] << endl; 44 return result; 45 } 46 };
Leetcode-926 Flip String to Monotone Increasing(将字符串翻转到单调递增)
标签:code 字符串 col pre size lips tco vector end
原文地址:https://www.cnblogs.com/Asurudo/p/9838310.html