当前位置:网站首页>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);
}
}边栏推荐
- 第48篇-timestamp2参数分析【2022-08-01】
- String concatenation in SQL
- 节省50%成本!京东云重磅发布新一代混合CDN产品
- "Second Uncle" is popular, do you know the basic elements of "exploding" short videos from the media?
- photo-sphere-viewer Chinese documentation
- 百日刷题计划 ———— DAY1
- 【C语言】细品分支结构——switch语句
- 3 ways for OpenFeign to set headers
- [typescript] Use the RangePicker component in antd to implement time limit the previous year (365 days) of the current time
- [b01lers2020]Welcome to Earth-1
猜你喜欢
随机推荐
scrapy框架初识1
最小割和对偶图(未完成)
[C language] Analysis of function recursion (2)
How to do short video food from the media?5 steps to teach you to get started quickly
机器人碰撞检测方法形式化
【C语言】手撕循环结构 —— for语句
Mysql视图
二叉树的类型、构建、遍历、操作
Mysql索引详解(图文并茂)
基于flask商城的管理员功能
Markdown怎么加入emoji
Based on the flask mall administrator functions
[C language] Analysis of function recursion (3)
In-depth analysis and use of Ribbon load balancing
SQL Server 2019 installation error 0 x80004005 service there is no timely response to the start or control request a detailed solution
鲁大师7月新机性能/流畅榜:性能跑分突破123万!
Oracle数据库的闪回技术
Singleton pattern of seven kinds of writing, you know?
【C语言】手撕循环结构 —— while语句
PGSQL database to realize the import and export









