Easy to understand , a simple java solution


  • 0
    S
    public class Solution {
       int p = 0;
        public String removeKdigits(String num, int k) {
            List<Character> list = new ArrayList<>();
            int i = 0;
            boolean preIsZero = true;
            StringBuilder stringBuilder = new StringBuilder();
            while (k < num.length()) {
                char min = getMin(num.toCharArray(), i, k);
                if (preIsZero) {
                    if (min != '0'){
                        stringBuilder.append(min);
                        preIsZero = false;
                    }
                } else
                    stringBuilder.append(min);
                i = p + 1;
                k++;
            }
            
            if (preIsZero || stringBuilder.length() == 0)
                return "0";
            return stringBuilder.toString();
        }
    
        private char getMin(char[] a, int i, int j) {
            char min = a[i];
            p = i;
            for (int k = i; k <= j; k++) {
                if (a[k] < min) {
                    min = a[k];
                    p = k;
                }
            }
            return min;
        }
        
    }
    
    
    

Log in to reply
 

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