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 ...
随机推荐
- html布局-子div浮动后,父容器撑不开解决
文章:子div撑不开父div的几种解决办法: 1,可以在所有子元素后增加<div style="clear:both;"></div> 2,给父容器增加样式 ...
- 清除PLSQL Developer访问数据库连接的历史记录
1.C盘下 路径: C:\Users\JourneyOfFlower\AppData\Roaming\PLSQL Developer 12\Preferences\JourneyOfFlower\ J ...
- 深度学习算法 之DCGAN(写得不系统,后期再总结,大家可简单阅览一下)
目录 1.基本介绍 2.模型 3.优缺点/其他 参考 1.基本介绍 DCGAN是生成对抗网络GAN中一种常见的模型结构.其中的生成器和判别器都是神经网络模型. GAN是一种生成式对抗网络,即通过对抗的 ...
- 题解 洛谷P4302 【[SCOI2003]字符串折叠】
一眼区间\(dp\),但蒟蒻的我还是调了好久\(qwq\) [状态设置] 设\(f[i][j]\)为子串\([i,j]\)的最短折叠 最后答案为\(f[1][n]\) 废话 [初始化] \(1\) 首 ...
- 《Exceptioning团队》第四次作业:项目需求调研与分析
一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.探索团队软件项目需求获取技巧与方法2.学会 ...
- npm run build 时的 warning
entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit ...
- js比较日期大小 判断日期
使用js的方法来判断两个日期的先后关系,不能正常判断,因此手写了一个方法,如下: //判断开始日期是否大于结束日期,注意,该方法仅仅适用于“2010-01-01”这样的日期格式! function ...
- C#使用托管程序连接Oracle数据库(Oracle.ManagedDataAccess.dll)
一.概述 Oracle Data Provider for .NET, Managed Driver: Oracle官方的托管数据库访问组件,单DLL,Oracle.ManagedDataAcces ...
- Mycat简单配置
最近项目中需要用到Mycat来作为Mysql的分表中间件.所以稍微研究了一下. Mycat使用起来是非常方便,而且最重要的是配置简单,稍显麻烦的就是需要对库中的每一个表都进行配置. 记录一下最重要的几 ...
- Yarn节点及作用
1.yarn中的角色:ResourceManager.NodeManager.ApplicationMaster. ResourceManager:集群计算资源的分配,启动ApplicationMas ...