当前位置:网站首页>CSDN(成长一夏竞赛)- 最大数
CSDN(成长一夏竞赛)- 最大数
2022-08-02 13:13:00 【放羊的牧码】
题目大意
给定任意一个数字 m,然后给出数字 n,则需在 m 中去掉 n 位数,保持各位顺序不变的情况下,得到最大数。
输入描述
输入整数n,m (1<=n<=1e100,1<=m<=100)
输出描述
输出删除后的最大数。
示例
输入:1234 2
输出:34
解题思路
- 计算出结果坑位数(size = n.length - m)
- 每一个坑位数的最大值下标[0, size]、[0, size + 1]、[0, size + 2]……
- 最难理解在第二点,因为题目说保障前后顺序,所以比如 12345 2,那么,坑位数为 3,第一个位置的最大值在[0, 2],第二个[0, 3],第三个[0, 4],中途如果被用过的数字需要做下标记,后面的坑位不能再使用
相关企业
- CSDN
AC 代码
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str_0 = scan.nextLine();
String[] line_list_0 = str_0.trim().split(" ");
ArrayList<String> arr = new ArrayList<>();
for(int i = 0; i < line_list_0.length; i++){
arr.add(line_list_0[i]);
}
scan.close();
String result = solution(arr);
System.out.println(result);
}
public static String solution(ArrayList<String> arr){
String str = arr.get(0);
int cnt = Integer.valueOf(arr.get(1));
int len = str.length();
int diff = len - cnt;
char[] chars = new char[diff];
char[] pre = str.toCharArray();
for (int i = 0, from = 0; i < diff; i++) {
// j = from,优化,因为下一个坑位数不可能在 from 之前
for (int j = from; j <= cnt + i; j++) {
if (chars[i] < pre[j]) {
chars[i] = pre[j];
from = j + 1;
}
}
}
return String.valueOf(chars);
}
}边栏推荐
- This binding to detailed answers
- Redis all
- 基于flask商城的管理员功能
- js数组递归使用
- How to implement waterfall flow layout (what is waterfall flow layout)
- Reading IDEO, Design Changes Everything
- Scala基础语法入门(三)Scala中的各种运算符
- 【typescript】使用antd中RangePicker组件实现时间限制 当前时间的前一年(365天)
- 3 ways for OpenFeign to set headers
- Good shooting js game source code
猜你喜欢

C语言结构体(入门)

鲁大师7月新机性能/流畅榜:骁龙8+正面对决天玑9000+,性能跑分突破123万!

分享一个Chrome控制台数据获取的例子

【C语言】剖析函数递归(3)

How to improve the originality of self-media creation and create popular works?

This binding to detailed answers

“二舅”火了,自媒体短视频“爆火”的基本要素,你知道吗?

Seata Distributed Transaction
![PHP+MYSQL [Student Information Management System] (Minimalist Edition)](/img/86/d5d39eef0600acabbf3ac16c255c18.png)
PHP+MYSQL [Student Information Management System] (Minimalist Edition)

国产 GPU 创业潮 喧嚣下的资本游戏
随机推荐
Based on the flask mall administrator functions
String concatenation in SQL
C语言结构体(入门)
Do you know Dijkstra of graph theory?
Set proxy server (Google+IE) "Recommended Collection"
Name conventions in FreeRTOS
[typescript] Use the RangePicker component in antd to implement time limit the previous year (365 days) of the current time
Redis all
【C语言】手撕循环结构 —— while语句
节省50%成本!京东云重磅发布新一代混合CDN产品
高效代码静态测试工具Klocwork 2022.2——Portal全新升级、支持RLM
How to connect DBeaver TDengine?
Wireless vibrating wire acquisition instrument remote modification method
RestTemplate 使用:设置请求头、请求体
图论之Prim,最小生成树该怎么解?
动态组件-component
WPF效果第一百九十三篇之登录实现
图论之Floyd,多源图最短路如何暴力美学?
我的创作纪念日
.Net 5.0 Quick Start Redis