Greedy: remove earliest down-edge: like "54", "97".

class Solution {
public:
/**
*@param A: A positive integer which has N digits, A is a string.
*@param k: Remove k digits.
*@return: A string
*/
string DeleteDigits(string A, int k)
{
size_t n = A.size(); int cnt = ; int i = ;
while(i < n - )
{
if(A[i] > A[i + ])
{
A.erase(i, );
if(i > ) i--;
n --;
if(++cnt == k) break;
}
else
{
i ++;
}
}
if(cnt < k) // all ascending, remove last
{
n = A.size();
A = A.substr(, n - (k - cnt));
} // Remove leading 0s
i = , n = A.size();
while(i < n && A[i] == '') i ++;
A = A.substr(i);
if(A.empty()) A = ""; return A;
}
};

LintCode "Delete Digits"的更多相关文章

  1. [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点

    Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...

  2. LintCode: Delete Node in the Middle of Singly Linked List

    开始没看懂题目的意思,以为是输入一个单链表,删掉链表中间的那个节点. 实际的意思是,传入的参数就是待删节点,所以只要把当前节点指向下一个节点就可以了. C++ /** * Definition of ...

  3. git文章列表

    关于gitlab默认clone协议 Git实现从本地加入项目到远程仓库 翻翻git之---一个简单的标签控件 LabelView (随手发了两张小宝宝的玩耍照) id=1125" targe ...

  4. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  5. CodeForces 327C

    Magic Five Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit  ...

  6. 打印 1 到最大的 n 位数(C++ 和 Python 实现)

    (说明:本博客中的题目.题目详细说明及参考代码均摘自 “何海涛<剑指Offer:名企面试官精讲典型编程题>2012年”) 题目 输入数字 n,按顺序打印出从 1 到最大的 n 位十进制数. ...

  7. BigInt的实现——C++编程风格读书笔记

    C++编程风格这本书前面一些章节都觉得很简明易懂,但是读到效率这一章是才充分认识到读别人的代码还是很痛苦的一件事.书中给出的需要改进的初始类如下: class BigInt { private: ch ...

  8. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  9. 372. Delete Node in a Linked List【LintCode java】

    Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...

随机推荐

  1. TC HTB r2q

    HTB: quantum of class 10001 is big. Consider r2q change. 根据HTB的官方文档显示,quantum是在可以“借”的情况下,一次可以“借”多少,并 ...

  2. DPM(Deformable Parts Model)--原理(一)(转载)

    DPM(Deformable Parts Model) Reference: Object detection with discriminatively trained partbased mode ...

  3. 274. H-Index

    Given an array of citations (each citation is a non-negative integer) of a researcher, write a funct ...

  4. 修饰符(static、final、abstract)第二篇

    二.Final 核心:一旦创建,不可修改 可修饰:(创建即终态) 1.类:被修饰后,将不能被继承 2.方法:被修饰后,将不能被覆写 3.属性:被创建,不可修改,且必须赋值(赋值的两种形式): 1.直接 ...

  5. Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  6. tyvj1017 - 冗余关系 ——并查集

    题目链接:https://www.tyvj.cn/Problem_Show.aspx?id=1017 并查集 #include <cstdio> #include <cstdlib& ...

  7. tiny4412SD启动盘的制作--1

    一.使用SD-flasher工具烧写superboot到SD卡. 1.SD-Flasher.exe 会对 SD 卡进行分区,第一个分区为 130M 用于存放 Superboot4412, 剩下的空间格 ...

  8. JavaWeb学习记录(十九)——jsp标签库

    1.out标签 <%        //局部变量        String name="zsf>&<zz";        pageContext.se ...

  9. 课堂所讲整理:super和转型(修改版)

    创建父类: package org.hanqi.pn0120; public class Father { private String name; private int age; public S ...

  10. HDU-4521 小明系列问题――小明序列(线段树)

    题目大意:求LIS,但是要求LIS中相邻的两个元素之间的距离要大于d. 题目分析:线段树.节点(l,r)保存信息为LIS的最后一个元素落在[l,r]之间的最大长度.从第d+2个元素开始查询更新操作,但 ...