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 + nfrom 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的更多相关文章

  1. 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 ...

  2. 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)

    [LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  3. [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 ...

  4. 738. Monotone Increasing Digits 单调递增的最接近数字

    [抄题]: Given a non-negative integer N, find the largest number that is less than or equal to N with m ...

  5. 738. Monotone Increasing Digits

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  6. 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 ...

  7. [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 ...

  8. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  9. [LeetCode] Monotone Increasing Digits 单调递增数字

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

随机推荐

  1. 基于easyui开发Web版Activiti流程定制器详解(六)——Draw2d详解(二)

    回顾: 上一篇我们介绍了Draw2d整体结构,展示了组件类关系图,其中比较重要的类有Node.Canvas.Command.Port.Connection等,这篇将进一步介绍Draw2d如何使用以及如 ...

  2. JVM虚拟机20:内存区域详解(Eden Space、Survivor Space、Old Gen、Code Cache和Perm Gen)

    1.内存区域划分 根据我们之前介绍的垃圾收集算法,限定商用虚拟机基本都采用分代收集算法进行垃圾回收.根据对象的生命周期的不同将内存划分为几块,然后根据各块的特点采用最适当的收集算法.大批对象死去.少量 ...

  3. Django在admin模块中显示auto_now_add=True或auto_now=True的时间类型列

    转载自: http://www.tuicool.com/articles/ZryE7f 在Django如果model中的列定义了auto_now_add或auto_now属性,那么这种列不会在admi ...

  4. hadoop学习;hdfs操作;执行抛出权限异常: Permission denied;api查看源代码方法;源代码不停的向里循环;抽象类通过debug查找源代码

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u010026901/article/details/26587251 eclipse快捷键alt+s ...

  5. Python文件和流

    #coding = utf-8 from pprint import pprint import fileinput #read(n) f = open(r'E:\test_dir\somefile. ...

  6. datagrid 完整dom结构

    <!-- datagrid的最外层容器,可以使用$(target).datagrid('getPanel')或者$.data(target,'datagrid').panel得到这个DOM对象, ...

  7. Intellij IDEA的激活(2100年你值得拥有)

    下载ide官网地址:https://download.jetbrains.com/idea/ideaIU-2018.2.7.exe 安装下一步下一步:进入安装bin目录 首先下载需要破解的jar包链接 ...

  8. Jenkins+github的一次定时构建示例

    首先说明,我的电脑环境是windows,所以以下的示例是基于windows10 X64. 一.新建任务,填写名称,选择类型,点击左下角的[确定] 二.配置 1.General 2.源码管理 之前在gi ...

  9. [原创]升级Gerrit的commit-msg,检查git commit时必须填写开发任务编号TaskID

    公司使用git+gerrit+jenkins进行持续集成实践,其中gerrit用来进行Code Review.另外我们自己研发了一套敏捷项目管理系统TPM(TeamPlus Management),用 ...

  10. redis 数据库随笔 (一)

    redis数据库的基本类型分析: 1.string 最基本的数据类型.只存贮一个值,key-value,最大值存储512M. 创建命令:hmset  读取命令:hget 2.hash 集合,存储为一个 ...