LintCode "Delete Digits"

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 = 0;

        int i = 0;
        while(i < n - 1)
        {
            if(A[i] > A[i + 1])
            {
                A.erase(i, 1);
                if(i > 0) i--;
                n --;
                if(++cnt == k)    break;
            }
            else
            {
                i ++;
            }
        }
        if(cnt < k) // all ascending, remove last
        {
            n = A.size();
            A = A.substr(0, n - (k - cnt));
        }

        //  Remove leading 0s
        i = 0, n = A.size();
        while(i < n && A[i] == ‘0‘) i ++;
        A = A.substr(i);
        if(A.empty()) A = "0";

        return A;
    }
};
时间: 2024-08-07 16:59:58

LintCode "Delete Digits"的相关文章

[lintcode medium] Delete digits

Delete Digits Given string A representative a positive integer which has N digits, remove any k digits of the number, the remaining digits are arranged according to the original order to become a new positive integer. Find the smallest integer after

Delete Digits

Given string A representative a positive integer which has N digits, remove any k digits of the number, the remaining digits are arranged according to the original order to become a new positive integer. Find the smallest integer after remove k digit

[LeetCode]Delete Digits

题目 Given string A representative a positive integer which has N digits, remove any k digits of the number, the remaining digits are arranged according to the original order to become a new positive integer. Make this new positive integers as small as

git文章列表

关于gitlab默认clone协议 Git实现从本地添加项目到远程仓库 翻翻git之---一个简单的标签控件 LabelView (随手发了两张小宝宝的玩耍照) Git 项目推荐 | Java 版微信普通号机器人 翻翻git之---炫酷的自定义翻滚View TagCloudView GitHub连击500天:让理想的编程成为习惯 git初基本用法总结 xcode 自带的git工具创建项目流程 翻翻git之---编译器般高大上的WebView RichEditor (PS:家里两个小祖宗大爆照)

微软2014实习生在线测试之K-th string

问题描述: Time Limit: 10000msCase Time Limit: 1000msMemory Limit: 256MB Description Consider a string set that each of them consists of {0, 1} only. All strings in the set have the same number of 0s and 1s. Write a program to find and output the K-th str

CodeForces 327C

Magic Five Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the

lintcode 容易题:Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点

题目: 在O(1)时间复杂度删除链表节点 给定一个单链表中的表头和一个等待被删除的节点(非表头或表尾).请在在O(1)时间复杂度删除该链表节点.并在删除该节点后,返回表头. 样例 给定 1->2->3->4,和节点 3,返回 1->2->4. 解题: 方法好贱,先把要删除节点后面一个节点的值赋给删除节点,这样需要删除节点就删除了,再把删除节点指向删除节点后面节点的节点 就像这样: node.val = node.next.val; node.next = node.next.

LintCode - Remove Duplicates from Sorted List

LintCode - Remove Duplicates from Sorted List LintCode - Remove Duplicates from Sorted List Web Link Description Code - C Tips Web Link http://www.lintcode.com/en/problem/remove-duplicates-from-sorted-list/ Description Given a sorted linked list, del

[LintCode] Remove Node in Binary Search Tree

Remove Node in Binary Search Tree Given a root of Binary Search Tree with unique value for each node.  Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still