My java Solution with Stack


  • 0
    W
    public String removeKdigits(String num, int k) {
            Stack<Character> stack = new Stack<>();
            for(int i = 0; i < num.length(); i++) {
                while(!stack.empty() && stack.peek() > num.charAt(i) && k > 0) {
                    stack.pop();
                    k--;
                } // remove front digits larger than current digit.
                if(i < num.length() - 1 && num.charAt(i) > num.charAt(i + 1) && k > 0) {
                    k--; // if current digit larger than next digit, remove
                } else {
                    stack.push(num.charAt(i));
                }
            }
            for(int i = 0; i < k; i++) stack.pop();
            String ans = "";
            while(!stack.empty()) {
                ans = stack.pop() + ans;
            }
            int index = 0;
            while(index < ans.length() && ans.charAt(index) == '0') index++;
            return index == ans.length()? "0" : ans.substring(index);
        }

Log in to reply
 

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