[力扣] 算法 1544 (C#)

1544. 整理字符串

public class Solution
{
    public string MakeGood(string s)
    {
        var deque = new LinkedList<char>();
        foreach(var ch in s)
            if (deque.Count != 0 &amp;&amp; test(deque.Last.Value, ch))
                deque.RemoveLast();
            else
                deque.AddLast(ch);
        return string.Concat(deque);
    }

    public static bool test(char a ,char b)
    {
        if (a > 'Z')
            (a, b) = (b, a);
        return b - a == 'a' - 'A';
    }
}

发表评论