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. UVa 10624 - Super Number

    题目大意 给定两个数n和m,如果长度为m的数满足对于每个i(n<=i<=m),数字的前i位都能被i整除,那么这个数就是超级数,求出字典序最小的符合要求的超级数. 分析 直接暴力搜索 #in ...

  2. ubuntu Virtualbox菜单栏不见

    ubuntu 装了Virtualbox 后,不知道怎么操作的导致顶部菜单栏不见啦, 网上查了下,我们看到开启/关闭 Scale Mode的快捷键都是 Ctrl C ,注意Ctrl是右边的那个不是左边那 ...

  3. Linux磁盘文件的命名

    磁盘的常用接口有两种:IDE和SATA接口,目前主流的是SATA接口. IDE接口由IDE扁平电缆线连接,一个电缆可连接两个IDE接口,通常主机又都会提供两个IDE接口,因此最多可以接到四个IDE设备 ...

  4. 运动历史图(MHI)——Motion History Image

    MHI最初是由Bobick 和 Davis提出的,在此之前,Bobick 和 Davis 首先提出了二值的运动能量图(Motion Energy Image,MEI),通过描述物体如何移动和运动在空间 ...

  5. 209. Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  6. Android模仿QQ空间图片上传——原理

    MainActivity.class package com.example.imagedemotext; import java.io.ByteArrayOutputStream; import j ...

  7. c 深度剖析 6

    函数的编码风格 1.注释 2,空行 3,缩进 4,参数长度,代码长度,语句长度要合适. 5,少用全局变量 6,指针仅作输入参数时,可用const 设置其为只读属性,避免其在函数中被修改. 7,函数默认 ...

  8. ctypes 模块

    ctypes赋予了python类似于C语言一样的底层操作能力,通过ctypes模块可以调用动态链接库中的导出函数.构建复杂的c数据类型. ctypes提供了三种不同的动态链接库加载方式:cdll(), ...

  9. 【转】 iOS 开发之静态库.a和动态库详解 -- 不错

    原文网址:http://blog.csdn.net/lxl_815520/article/details/52154331 一, 简单介绍 1.什么是库 库是程序代码的集合,是共享程序代码的一种方式 ...

  10. javascript闭包实例

    实例一 //每次执行一次c()i加1.关键在于var c=a():c容器将i装载记住了. function a(){ var i=0; function b(){ alert(++i); } retu ...