1. 程式人生 > >Leetcode: 402. Remove K Digits

Leetcode: 402. Remove K Digits

描述

c++程式碼:

class Solution {
public:
    string removeKdigits(string num, int k) {
            string ANS;
            int keep = num.size() - k;
            for (int i=0; i<num.size(); i++) {
                while (ANS.size()>0 && ANS.back()>num[i] && k>0) {
                    ANS.pop_back();
                    k--;
                }
                ANS.push_back(num[i]);
            }
            ANS.erase(keep, string
::npos); int s = 0; while (s<(int)ANS.size()-1 && ANS[s]=='0') s++; ANS.erase(0, s); return ANS =="" ? "0" : ANS; } };

演算法:

**利用貪婪演算法的思想,將去掉k次最小轉化為連續去掉k次1個數字,每次都最小。去掉1個數字最小也就是去掉從左向右檢索的第一個產生降序的數字,即產生最小結果。比如1213,去掉2結果最小。整體思路是利用一個棧儲存每一位數字,判定到達產生降序的數字時,就去掉該數字,算作去掉1個數字,此時再檢查當前棧頂元素是不是新的產生降序的數字。直到k個數字刪除結束。

相關推薦

LeetCode 402: Remove K Digits

spa amp tco sta size empty not zeros div Note: 1. Find a increasing digits number. It‘s kind of longest increasing subsequence but with f

LeetCode-402.Remove K Digits

0.原題 Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the sma

LeetCode-402. Remove K Digits

https://leetcode.com/problems/remove-k-digits/description/ Given a non-negative integer num represented as a string, remove k di

python leetcode 402. Remove K Digits

dfs或者用棧比大小 估計用dfs會超時 最後注意要去除字串開頭的0 class Solution(object): def removeKdigits(self, num, k): """ :type num: str :type

leetcode 402 Remove K Digits(移除k個數字 貪心)

分析和思路: 就是說給一個字串形式的正數,刪除k個數字,使得字串數字最小 貪心思路 那麼就是每次刪除一個數,每次保證高位最小。 java程式碼: class Solution { public String removeKdigits(String num

Leetcode: 402. Remove K Digits

c++程式碼: class Solution { public: string removeKdigits(string num, int k) { string ANS; int keep =

leetcode402. Remove K Digits

.com self. 一個 方法 enume spa get pre enumerate 題目如下: 解題思路:我的方法是從頭開始遍歷num,對於任意一個num[i],在[i+1~len(num)-1]區間內找出離num[i]最近並且小於num[i]的數num[j],如果

402. Remove K Digits - Medium

Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possib

演算法課第6周第1題——402. Remove K Digits

題目描述: Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest

008-演算法面試必備-Remove K Digits(待修改)

leetcode 402題農行軟開的筆試題Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is t

Remove K Digits 去除重複字母

給定一個僅包含小寫字母的字串,去除字串中重複的字母,使得每個字母只出現一次。需保證返回結果的字典序最小(要求不能打亂其他字元的相對位置)。 示例 1: 輸入: "bcabc"輸出: "abc" 示例 2: 輸入: "cbacdcbc"輸出: "acdb" 思路:

Leetcode 402.移掉k位數字

移調k位數字 給定一個以字串表示的非負整數 num,移除這個數中的 k 位數字,使得剩下的數字最小。 注意: num 的長度小於 10002 且 ≥ k。 num 不會包含任何前導零。 示例 1 : 輸入: num = "1432219", k = 3

LeetCodeRemove Nth Node From End of List && 【九度】題目1517:連結串列中倒數第k個結點

      Total Accepted: 8400 Total Submissions: 28316       Given a linked list, remove the nth node from the end of list and return its h

[LeetCode][Java] Remove Duplicates from Sorted List II

gin -m || 代碼 number 算法分析 add dup adding 題意: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only

leetCode 27.Remove Element (刪除元素) 解題思路和方法

value ava leetcode ont bsp lac 方法 -s post Remove Element Given an array and a value, remove all instances of that value in plac

LeetCode 23 Merge k Sorted Lists

lee .cn cnblogs analyze 優先權 bool log sin node DescriptionMerge k sorted linked lists and return it as one sorted list. Analyze and descri

Leetcode----27 Remove Element

.com div doesn change extra href leave func begin Description Given an array and a value, remove all instances of that value in place an

leetCode 83.Remove Duplicates from Sorted List(刪除排序鏈表的反復) 解題思路和方法

排序 back ace 去除 adding 思路 詳細 init ica Given a sorted linked list, delete all duplicates such that each element appear only once.

[Leetcode] 27 Remove Element

not leetcode can leet pac div mov sta http 原題地址: https://leetcode.com/problems/remove-element/description/ 題目: Given an array and a val

LeetCode 80. Remove Duplicates from Sorted Array II

ray pxn oci rfs remove sorted rem mar target 哨28環尤6乜xn是墳暇8http://t.docin.com/phkx158 Q0埔68阜氛勺資chttp://t.docin.com/sina_6272984987 繃梢陡紊6