腾讯后端笔试题(消消乐游戏)请求解答

## 20210404腾讯后台笔试题提问

**结果:只能通过78.57%的case,但是测试案例和自己挑选的测试案例都能过,求大神解答?**
*题目描述*:消消乐游戏,有—个长度为n的仅有1-9这9个数字组成的串。如果相邻两个数之和为10、这这两个数可以彼此抵消,在原串中抹去.例:串213792,第三个数3和其相邻的第4个数7两者之和秘10,因此可以消法;原串2192,1和9之和为10,且相邻,则可以消去,最终变为22。
       求这个长度为n的串最小可以被消成长度为多少的串
*输入描述*:  
第一行输入一个整数n(1=n<= le6), 代表串的长度。
接下来输入一个长度为n的仅有1~9组成的数字串。
输出描述:
输出这个长度为 n的串最小可以被消成长度为多少的串。

案例1:
输入:
6
213792
输出:
2

案例2:
输入:
7
2134314
输出:
7

案例3:
输入:
6
146937
输出:
0

思路:利用栈后入先出实现
```java
import java.util.Scanner;
import java.util.Stack;

public class Test {
    Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        Main m = new Main();
        int strLength = m.sc.nextInt();
        String str = m.sc.next();
        int[] nums = new int[strLength];
        Stack<Integer> s = new Stack<>();
        for(int i = 0; i < strLength; i++)
        {
            char c = str.charAt(i);
            char[] charArray = {c};
            nums[i] = Integer.parseInt(new String(charArray));
        }
        s.push(nums[0]);
        int count = 1;
        while(true)
        {
            if(count == strLength) break;
            while( !s.empty() && ((s.peek().intValue()+ nums[count]) == 10))
            {
                s.pop();
                count++;
            }
            if(count == strLength) break;
            s.push(nums[count++]);
        }
        System.out.println(s.size());
        return;

    }
}
```

爱莫能助啊