lintcode-medium-Delete Digits
Posted 哥布林工程师
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode-medium-Delete Digits相关的知识,希望对你有一定的参考价值。
Given string A representative a positive integer which has N digits, remove any k digits of the number, the remaining digits are arranged according to the original order to become a new positive integer.
Find the smallest integer after remove k digits.
N <= 240 and k <= N
Given an integer A = "178542"
, k = 4
return a string "12"
贪心算法
public class Solution { /** *@param A: A positive integer which has N digits, A is a string. *@param k: Remove k digits. *@return: A string */ public String DeleteDigits(String A, int k) { // write your code here if(A == null || A.length() == 0) return A; String str = A; for(int i = 0; i < k; i++){ int index = 0; for(; index < str.length() - 1; index++){ if(str.charAt(index) > str.charAt(index + 1)) break; } str = str.substring(0, index) + str.substring(index + 1); } while(str.charAt(0) == ‘0‘) str = str.substring(1); return str; } }
以上是关于lintcode-medium-Delete Digits的主要内容,如果未能解决你的问题,请参考以下文章
php Customizzare la pagina di login di Wordpress