整数删除数字问题

 

问题描述:给出一个正整数v ,去掉其中的c个数,使得剩下的数组成的值最大
                  例如:v = 351;c = 1;则结果是51

解答思路:动态规划,一层一层的找出当前操作的最大值。(先找出当前数去掉一个数字的最大值,依次循环,找出下一个……,直到去掉c个数后的最大值)

 1     public int removeNumber(int v,int c){
 2         String strV = String.valueOf(v);
 3         String current = strV.substring(0,strV.length());
 4         String  result = strV.substring(0,strV.length());
 5         for(int i=0;i<c;i++){
 6             String compare = current.substring(0,current.length());
 7             current = current.substring(0, current.length()-1);
 8             int size = result.length();
 9             for(int j=0;j<size-1;j++){
10                 String temp = compare.substring(0,j)+compare.substring(j+1,size);
11                 if(current.compareTo(temp)<0){//找出最大字符串
12                     current = temp;
13                 }
14             }
15             result = current;
16         }
17         return Integer.parseInt(result);
18     }

 

posted on 2018-03-15 15:30  一只愤怒的橘子  阅读(228)  评论(0编辑  收藏  举报

导航