Java Accept Solution


  • 0
    L

    The running time is not that fast, since I just use string concatenate.
    The method is simple, every time we scan from the beginning to find the first peak element and remove it. Loop it for K times.

       public String removeKdigits(String num, int k) {
           if(k >= num.length()) return "0";
           int i = 0;
           while(k > 0){
               i = 0;
               for(; i < num.length()-1 && num.charAt(i) <= num.charAt(i+1);i++);
               num = num.substring(0, i) + (i < num.length()-1? num.substring(i+1):"");
               k--;
           }
           //get rid of heading '0'
           for(i = 0; i < num.length() && num.charAt(i) == '0';i++);
           return i == num.length()? "0":num.substring(i);
       }
    

Log in to reply
 

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