#leetCode brush title documentary Day1

https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/

String S is given by the lowercase letters, delete duplicates and selects two identical letters adjacent to, and remove them.

Performed on the S repeatedly duplicates deletion, can not continue until deleted.

Returns the final string after the completion of all duplicates deletion. The only answer guarantee.

 

Example:

Enter: "abbaca"
Output: "ca"
interpretation:
for example, in "abbaca", we can remove the "bb" as the two adjacent letters and the same, this is the only duplicates this time can perform delete operations. After we get the string "aaca", which has only "aa" can perform delete duplicates, so the final string is "ca".
 

prompt:

. 1 <= s.length <= 20000
S only by the lower case letters.

 

Chicken dishes first pass of independence and self-improvement:

 1 class Solution {
 2 public:
 3     string removeDuplicates(string S) {
 4         start: int length = S.length();
 5         for (int i = 0; i < length - 1; i ++) {
 6             if (S[i] == S[i + 1]) {
 7                 S = S.substr(0, i) + S.substr(i + 2, length - i - 2);
 8                 goto start;
 9             }
10         }
11         return S;
12     }
13 };

The principle is very simple, that loop through the string, each time a character and the current character backward compared, it will be the same if the front and rear sections are connected together, the string (removal of two identical characters). To achieve control of the cycle by a goto statement.

(Although chicken dishes using goto know it is not a good habit, but really do not know how to write here without goto) (No harm when used, the way the chicken dish ah)

 

 

 

Chicken dishes began to see bigwigs Solution:

 

Chicken dishes wondering: "This is what I wrote, like O (N 2 ) algorithm ah (but this idea very clever), but like c ++ string can find no direct method substring ah, I try to use js ! "

(Also learned how not to use the goto (because there is no js goto ah!))

 1 var removeDuplicates = function(S) {
 2     var list = ["aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn", "oo", "pp", "qq", "rr", "ss", "tt", "uu", "vv", "ww", "xx", "yy", "zz"];
 3     prev_length = -1;
 4     while (prev_length != S.length) {
 5         prev_length = S.length;
 6         var length = list.length;
 7         for (var i = 0; i < length; i ++) {
 8             var po = S.indexOf(list[i]);
 9             if (po != -1) {
10                  S = S.substr(0, po) + S.substr(po + 2, length - po - 2);
11             }
12         }
13     }
14     return S;
15 };

 

Chicken dishes continue to look bigwigs Solution:

 


 

 1 class Solution {
 2 public:
 3     string removeDuplicates(string S) {
 4         stack<char> result;
 5         int length = S.length();
 6         result.push(S[0]);
 7         for (int i = 1; i < length; i ++) {
 8             if (result.empty() || S[i] != result.top()) {
 9                 result.push(S[i]);
10             } else {
11                 result.pop();
12                 continue;
13             }
14         }
15         string re;
16         int size = result.size();
17         for(int i = 0; i < size; i ++) {
18             re = result.top() + re;
19             result.pop();
20         }
21         return re;
22     }
23 };

He is an excellent O (N) algorithm! ! !

 

 

 

 

 

 

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Guess you like

Origin www.cnblogs.com/xyy999/p/11756424.html
Recommended