当前位置:网站首页>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);
}
}边栏推荐
- Reading IDEO, Design Changes Everything
- 移动端适配,华为浏览器底色无法正常显示
- ThinkPHP 5.1反序列化分析和poc
- Detailed explanation of network flow (what information can the flow network diagram generally reflect)
- RISC-V 指令格式和6种基本整数指令
- SQL Server database generation and execution of SQL scripts
- Mysql index details (with pictures and texts)
- [C language] Analysis of function recursion (3)
- 【C语言】手撕循环结构 —— while语句
- Based on the flask mall administrator functions
猜你喜欢

SQL Server 2019 installation error 0 x80004005 service there is no timely response to the start or control request a detailed solution
![[C language] Analysis of function recursion (2)](/img/b1/9baa60a69d41a1823ed92001d1b0a0.png)
[C language] Analysis of function recursion (2)

First acquaintance of scrapy framework 1

scrapy框架初识1

C语言结构体(入门)

In-depth analysis and use of Ribbon load balancing

【C语言】细品分支结构——if-else语句

高效代码静态测试工具Klocwork 2022.2——Portal全新升级、支持RLM

photo-sphere-viewer Chinese documentation

SQL Server 2014 installation tutorial (nanny-level graphic tutorial)
随机推荐
智能手表前景如何?
Reading IDEO, Design Changes Everything
Wireless vibrating wire acquisition instrument remote modification method
Intouch Historian历史曲线配置导入导出
RESTful style (detailed introduction + case implementation)
定了!2022世界VR产业大会将继续在南昌召开
RISC-V instruction format and 6 basic integer instructions
Based on the flask mall administrator functions
sql concat() function
读《IDEO,设计改变一切》有感
Win11怎么修改关机界面颜色?Win11修改关机界面颜色的方法
Interpretation of new features | MySQL 8.0 GIPK invisible primary key
路由-Tab切换页面
分享一个Chrome控制台数据获取的例子
LeetCode_139_word split
Js scratchable latex style draw plug-in
Flashback Technology of Oracle Database
this的绑定指向详细解答
js array recursively use
qt 编译报错 No rule to make target