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. 【整理】close 和 shutdown 的原理

    http://stackoverflow.com/questions/14740852/linux-socket-close-vs-shutdown shutdown(sd, SHUT_WR)  发送 ...

  2. 在远程登陆的主机上通过命令行源码编译安装 GNU M4、autoconf、automake 等程序

    由于实验需要,最近获得了一个实验室服务器的账号,平常主要通过 ssh 进行远程登陆进行实验.一方面,远程登录的机器只提供终端界面,一般只通过命令行进行任务操作:另一方面,由于是多人共享服务器,故而个人 ...

  3. 使用Linux进行缓冲区溢出实验的配置记录

    在基础的软件安全实验中,缓冲区溢出是一个基础而又经典的问题.最基本的缓冲区溢出即通过合理的构造输入数据,使得输入数据量超过原始缓冲区的大小,从而覆盖数据输入缓冲区之外的数据,达到诸如修改函数返回地址等 ...

  4. rabbitmq的万能安装和外网访问(NC版)

    先去这个http://www.rabbitmq.com/releases/下载erlang环境和rpm(erlang的尽量高点,rabbitmq版本差不多就可以了,) erlang-19.0.4-1. ...

  5. 2017-2018-2 20165318 实验二《Java面向对象程序设计》实验报告

    2017-2018-2 20165318 实验二<Java面向对象程序设计>实验报告 一.实验报告封面 课程:Java程序设计        班级:1653班        姓名:孙晓暄  ...

  6. jdk8环境下,添加重复注解的美好体验

    为了实现业务层缓存,定义了几个注解:@Cache.able.@Cache.put.@Cache.del 分别实现对业务方法的 缓存检测.缓存插入 和 缓存清除. public @interface C ...

  7. Hive学习之路 (十)Hive的高级操作

    一.负责数据类型 1.array 现有数据如下: 1 huangbo guangzhou,xianggang,shenzhen a1:30,a2:20,a3:100 beijing,112233,13 ...

  8. Ubuntu16.04如何彻底删除Apache2

    虽然作为运维人员通常情况不建议随意删除Linux系统上面的任何软件,主要指生产环境下,测试环境也不能太随意. 但是有的时候,比如系统环境要变一变,我们就需要替换一些淘汰的软件,对此我们一般都会删除. ...

  9. 关于NRF52832能否被替代的详解

    ULP无线系统级芯片 nRF52832是用于ULP无线应用的功能强大的多协议单芯片解决方案.它结合了业界性能最佳的Nordic最新无线收发器.ARM Cortex M4F CPU和512kB闪存及64 ...

  10. 关于ESP8266EX的一些资料

    乐鑫智能互联平台 ESP8266EX 拥有高性能无线 SOC,给移动平台设计师带来福⾳音,它以最低成本提供最大实用性,为 WiFi 功能嵌入其他系统提供无限可能. ESP8266EX 是⼀一个完整且⾃ ...