整数删除数字问题

 

问题描述:给出一个正整数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     }

 

转载于:https://www.cnblogs.com/zhq510/p/8573890.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值