leetcode 402. Remove K Digits 、321. Create Maximum Number
402. Remove K Digits
https://www.cnblogs.com/grandyang/p/5883736.html
https://blog.csdn.net/fuxuemingzhu/article/details/81034522
https://blog.csdn.net/qq508618087/article/details/52584133
题目的要求是删除k个字符,在保证原字符串字符相对位置不变的情况下获得最小的数字。
一个贪心的思想,维护一个单调递增的栈。只能弹出k次。
res.resize(keep)对于在while循环里删除了k次的没影响,但对于那些没有删除k次的就需要只提取前keep个数值。因为这是一个单调递增栈,同样keep位的数字,前面的生成的数字更小。
class Solution {
public:
string removeKdigits(string num, int k) {
string res = "";
int keep = num.size() - k;
for(char word : num){
while(k && res.size() && word < res.back()){
res.pop_back();
k--;
}
res.push_back(word);
}
res.resize(keep);
while(!res.empty() && res[] == '')
res.erase(res.begin());
return res.empty() ? "" : res;
}
};
321. Create Maximum Number
https://www.cnblogs.com/grandyang/p/5136749.html
这道题和402. Remove K Digits很类似,maxNumber函数基本上就是402. Remove K Digits,只是这道题是求最大的值,且给的k不是删除的个数,而是保留的个数。
将两个数组进行按照大小合并时,不能使用那种两个指针滑动的方式。
两个vector数组进行比较,比较的是第一个位置的数的大小,如果相等会比较第二个位置的大小,直到有大小区别。
以下这个代码就证明了。
#include <iostream>
#include <vector> using namespace std; int main(){
int a[] = {,};
int b[] = {,,};
vector<int> num1(a,a+);
vector<int> num2(b,b+);
if(num1 > num2)
cout << "debug" << endl;
}
整个的思路其实就是在nums1中选i个元素组成的最大的数,nums2选k-i个元素组成最大的数,然后进行组合。
class Solution {
public:
vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
int size1 = nums1.size(),size2 = nums2.size();
vector<int> res;
for(int i = max(,k - size2);i <= min(size1,k);i++){
res = max(res,mergeVector(maxNumber(nums1,i),maxNumber(nums2,k-i)));
}
return res;
}
vector<int> maxNumber(vector<int> nums,int k){
int del = nums.size() - k;
vector<int> res;
for(int i = ;i < nums.size();i++){
while(del && res.size() && nums[i] > res.back()){
res.pop_back();
del--;
}
res.push_back(nums[i]);
}
res.resize(k);
return res;
}
vector<int> mergeVector(vector<int> nums1,vector<int> nums2){
vector<int> res;
while(!nums1.empty() || !nums2.empty()){
if(nums1 > nums2){
res.push_back(nums1[]);
nums1.erase(nums1.begin());
}
else{
res.push_back(nums2[]);
nums2.erase(nums2.begin());
}
}
return res;
}
};
leetcode 402. Remove K Digits 、321. Create Maximum Number的更多相关文章
- [LeetCode] 402. Remove K Digits 去掉K位数字
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- leetcode 402. Remove K Digits
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- 402. Remove K Digits/738.Monotone Increasing Digits/321. Create Maximum Number
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 321. Create Maximum Number 解题方法详解
321. Create Maximum Number 题目描述 Given two arrays of length m and n with digits 0-9 representing two ...
- 321. Create Maximum Number
/* * 321. Create Maximum Number * 2016-7-6 by Mingyang */ public int[] maxNumber(int[] nums1, int[] ...
- [LeetCode] 321. Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- LeetCode 321. Create Maximum Number
原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...
- 321. Create Maximum Number (c++ ——> lexicographical_compare)
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
随机推荐
- Windows性能计数器监控实践
Windows性能计数器(Performance Counter)是Windows提供的一种系统功能,它能实时采集.分析系统内的应用程序.服务.驱动程序等的性能数据,以此来分析系统的瓶颈.监控组件的表 ...
- 用js刷剑指offer(第一个只出现一次的字符)
题目描述 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写). 牛客网链接 js代码 fu ...
- Java抽象语法树AST,JCTree 分析
JCTree简要分析文章目录JCTree简要分析JCAnnotatedTypeJCAnnotationJCArrayAccessJCArrayTypeTreeJCAssertJCAssignJCAss ...
- win10 64下anaconda4.2.0(python3.5)
python环境:win10 64下anaconda4.2.0(python3.5).安装tensorflow过程是在Anaconda Prompt中进行安装 1:打开Anaconda Prompt ...
- 第三天Beta冲刺
团队作业Beta冲刺 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 你们都是魔鬼吗 作业学习目标 (1)掌握软件黑盒测试技术:(2)学会编制软件 ...
- VBS读取txt文档数据查找Excel中单元格数据符合条件的剪切到工作表2中
Dim fso,f,a set oExcel = CreateObject( "Excel.Application" ) oExcel.Visible = false '4) 打开 ...
- 【Java】Unicode & UTF-8 & UTF-16 & UTF-32
Unicode Unicode(统一码.万国码.单一码)是计算机科学领域里的一项业界标准,包括字符集.编码方案等.Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设 ...
- POJ3709 K-Anonymous Sequence
题意 Language:Default K-Anonymous Sequence Time Limit: 4000MS Memory Limit: 65536K Total Submissions: ...
- VS Code中配置Markdown
其实,对我来说是反过来的,我是为了使用Markdown而安装VS Code(虽然久仰大名) 安装VS Code 安装Markdown插件 使用篇 1. 安装vscode 之所以啰嗦一下,是因为据说安装 ...
- URLSearchParams对象
URLSearchParams对象用于处理URL中查询字符串,即?之后的部分. 1.语法 其实例对象的用法和Set数据结构类似.实例对象本身是可遍历对象.但是不是遍历器. var paramsStri ...