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 the new number is the smallest possible.
Note:
- The length of num is less than 10002 and will be ≥ k.
- The given num does not contain any leading zero.
Example 1:
Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Example 2:
Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Example 3:
Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0.
class Solution {
public:
//使用栈
string removeKdigits(string num, int k)
{
if (k >= num.size())
return "";
string res = "";
int count = k;
for (char c : num)
{
while (count > && res.size() > && res.back() > c)
{
res.pop_back();
count--;
}
res.push_back(c);
}
res = res.substr(,num.length()-k);
while (res.empty()== false && res[] == '')
res.erase(res.begin());
return res.length()<= ? "" : res;
}
//深搜
vector<int> all;
string removeKdigits(string num, int k)
{
if (k >= num.length())
return "";
vector<int> bit(num.length(), );
vector<int> flag(num.length(), );
for (int i = ; i < bit.size(); i++)
bit[i] = (int)(num[i] - '');
getAll(,k, flag, bit);
int minRes = numeric_limits<int>::max();
for (int one : all)
minRes = min(minRes, one);
return to_string(minRes);
}
void dfs(int begin,int k,vector<int>& flag,vector<int>& bit)
{
if (k == )
{
int a = ;
for (int i = ; i < bit.size(); i++)
{
if (flag[i] == )
a = a * + bit[i];
}
all.push_back(a);
}
else if (begin >= flag.size())
return;
else
{
dfs(begin + , k, flag, bit);
flag[begin] = ;
dfs(begin + , k - , flag, bit);
flag[begin] = ;
}
}
};
738.leetcode: Monotone Increasing Digits
Given a non-negative integer N
, find the largest number that is less than or equal to N
with monotone increasing digits.
(Recall that an integer has monotone increasing digits if and only if each pair of adjacent digits x
and y
satisfy x <= y
.)
Example 1:
Input: N = 10
Output: 9
Example 2:
Input: N = 1234
Output: 1234
Example 3:
Input: N = 332
Output: 299
class Solution
{
public:
int monotoneIncreasingDigits(int n)
{
string num = to_string(n);
int begin = num.length();
for (int i = num.length() - ; i >= ; i--)
{
if (num[i] >= num[i - ])
continue;
else
{
num[i - ]--;
begin = i;
}
}
for (int i = begin; i < num.length(); i++)
num[i] = '';
return stoi(num);
}
};
321. Create Maximum Number:
Given two arrays of length m
and n
with digits 0-9
representing two numbers. Create the maximum number of length k <= m + n
from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k
digits.
Note: You should try to optimize your time and space complexity.
Example 1:
Input:
nums1 =[3, 4, 6, 5]
nums2 =[9, 1, 2, 5, 8, 3]
k =5
Output:
[9, 8, 6, 5, 3]
Example 2:
Input:
nums1 =[6, 7]
nums2 =[6, 0, 4]
k =5
Output:
[6, 7, 6, 0, 4]
Example 3:
Input:
nums1 =[3, 9]
nums2 =[8, 9]
k =3
Output:
[9, 8, 9]
class Solution
{
public:
vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k){
int m = nums1.size(), n = nums2.size();
vector<int> res;
// i的取值范围要小心
for (int i = max(, k - n); i <= min(k, m); ++i) {
res = max(res, mergeVector(maxVector(nums1, i), maxVector(nums2, k - i)));
}
return res;
}
// 栈的思想,求取k个数的最大值
vector<int> maxVector(vector<int> nums, int k) {
// 丢的方式比捡的方式实现
int drop = nums.size() - k;
vector<int> res;
for (int num : nums) {
while (drop && res.size() && res.back() < num) {
res.pop_back();
--drop;
}
res.push_back(num);
}
res.resize(k);
return res;
}
// 和有序数组外排有区别,求最大的归并值
vector<int> mergeVector(vector<int> nums1, vector<int> nums2) {
vector<int> res;
while (nums1.size() + nums2.size()) {
vector<int> &tmp = nums1 > nums2 ? nums1 : nums2;
res.push_back(tmp[]);
tmp.erase(tmp.begin());
}
return res;
}
};
402. Remove K Digits/738.Monotone Increasing Digits/321. Create Maximum Number的更多相关文章
- 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/fuxuemin ...
- 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)
[LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- [LeetCode] 738. Monotone Increasing Digits 单调递增数字
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- 738. Monotone Increasing Digits 单调递增的最接近数字
[抄题]: Given a non-negative integer N, find the largest number that is less than or equal to N with m ...
- 738. Monotone Increasing Digits
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- LC 738. Monotone Increasing Digits
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- [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 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- [LeetCode] Monotone Increasing Digits 单调递增数字
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
随机推荐
- [装]JMX监控Hadoop
http://chenjc-it.iteye.com/blog/1539746 实验成功!
- JavaScript创建对象的6种方式
JavaScript创建对象简单的说,无非就是使用内置对象(Object)或各种自定义对象,当然还可以用JSON,但写法有很多种,也能混合使用. 1.对象字面量的方式 person = {name : ...
- React 异步组件
之前写过一篇 Vue 异步组件的文章,最近在做一个简单项目的时候又想用到 React 异步组件,所以简单地了解了一下使用方法,这里做下笔记. 传统的 React 异步组件基本都靠自己实现,自己写一个专 ...
- Spring AbstractApplicationContext抽象类的refresh()方法--笔记
Spring中AbstractApplicationContext抽象类的refresh()方法是用来刷新Spring的应用上下文的.下面Spring的应用上下文我都叫作context @Overri ...
- 项目--解决MySQL数据库插入中文乱码
转载自:http://blog.csdn.net/zzh920625/article/details/51226312 情景再现] 如图,在项目中使用MySQL数据库,在做插入操作时,写入英文字符没有 ...
- Robot Framework自动化测试---Selenium API
一.浏览器驱动 通过不同的浏览器执行脚本. Open Browser Htpp://www.xxx.com chrome 浏览器对应的关键字: firefox FireFox ff internete ...
- BUAA OO 2019 第一单元作业总结
目录 总 架构 Controller Model 输入处理 代码静态分析 行数 方法复杂度 UML 类图 优点 缺点 坑 输入 非法的空白字符 输入的简并处理 运算 浅拷贝 可变类型与不可变类型 ...
- NLPIR(北理工张华平版中文分词系统)的SDK(C++)调用方法
一.本文内容简介 二.具体内容 1. 中文分词的基本概念 2.关于NLPIR(北理工张华平版中文分词系统)的基本情况 3.具体SDK模块(C++)的组装方式 ①准备内容: ②开始组装 三.注意事项 一 ...
- 移动端利用chrome浏览器在PC端进行调试方法
由于最近工作中遇到需要在电脑上调试手机端的功能和样式,之前也没有遇到过,所以就各种百度和试验.最后终于功夫不负有心人,成功了.(那一刻心情真滴很鸡冻啊~~~~~~~~~).所以暂时记录下来.以免鸡冻过 ...
- 记换换回收一个js逆向分析
随着现在对数据的重视程度越来越高,现在各大网站都加强了反爬技术,比如本文中js加密 url地址:https://www.huanhuanhuishou.com/gujia/22201.html 需要爬 ...