当前位置:网站首页>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);
}
}
边栏推荐
- js数组递归使用
- Introduction to Graph Neural Networks (GNN) "Recommended Collection"
- 基于华为eNSP的企业网络规划
- 国产 GPU 创业潮 喧嚣下的资本游戏
- 删除链表的节点
- Interpretation of new features | MySQL 8.0 GIPK invisible primary key
- RestTemplate 使用:设置请求头、请求体
- Win11怎么修改关机界面颜色?Win11修改关机界面颜色的方法
- 为什么IDEA连接mysql Unable to resolve table 编译报错但是可以运行
- 节省50%成本!京东云重磅发布新一代混合CDN产品
猜你喜欢
随机推荐
SQL Server 2019 installation error 0 x80004005 service there is no timely response to the start or control request a detailed solution
How to do short video food from the media?5 steps to teach you to get started quickly
The uniapp/applet onload method executes the interpretation every time the page is opened
MFC入门教程(深入浅出MFC)
js true 3d histogram plugin
供应磷脂-聚乙二醇-羧基,DSPE-PEG-COOH,DSPE-PEG-Acid,MW:5000
如何通过DBeaver 连接 TDengine?
svg balloon rises explosion js special effect
leetcode 504. Base 7 七进制数 (简单)
Summer training camp-week2 graph theory
php - the first of three solid foundations
Enterprise Network Planning Based on Huawei eNSP
【C语言】虐打循环练习题(2)
GCC版本升级到指定版本
[C language] Analysis of function recursion (2)
Set proxy server (Google+IE) "Recommended Collection"
汉源高科千兆12光12电管理型工业以太网交换机 12千兆光12千兆电口宽温环网交换机
Intouch System Platform IDE-1
LeetCode_139_word split
JS中的闭包