Missing test cas


  • 0

    I tried with this code:

        public String removeKdigits(String num, int k) {
            if(num == null) return null;
            int len = num.length();
            
            int i = 0;
            Deque<Character> stack = new ArrayDeque<>();
            for(i = 0; i < len && k > 0; i++){
                while( !stack.isEmpty() && num.charAt(i) < stack.peek() && k > 0){
                    stack.pop();
                    k--;
                }
                stack.push(num.charAt(i));
            }
            
            StringBuilder sb = new StringBuilder();
            while (!stack.isEmpty() && stack.peekLast() == '0') stack.pollLast(); 
            while(stack.size() > k){
                sb.append(stack.pollLast());
            }
            if(k == 0) sb.append(num.substring(i, len));
            return (sb.length() == 0)? "0": sb.toString();
        }
    

    And it got AC.
    However, for this test case:
    "10000012" 1
    it returns "000012".
    @administrators


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.