题目如下:

解题思路:我的方法是从头开始遍历num,对于任意一个num[i],在[i+1~len(num)-1]区间内找出离num[i]最近并且小于num[i]的数num[j],如果j-i <= k的话表示num[j]可以被删除,同时记k -= 1;如果找不到num[j]或者j-i > k则表示不能被删除。如果num遍历完成,但是k>0的话,把num最后k个字符删除,即为最终结果。

代码如下:

class Solution(object):
def getNearbyMin(self,d,val):
inx = 10002
for i in range(val):
i = str(i)
if i in d and len(d[i]) > 0:
inx = min(inx,d[i][0])
return inx
def removeKdigits(self, num, k):
"""
:type num: str
:type k: int
:rtype: str
"""
import bisect
res = ''
dic = {}
for i,v in enumerate(num):
if v not in dic:
dic[v] = [i]
else:
bisect.insort_left(dic[v],i) for i, v in enumerate(num):
nextMinInx = self.getNearbyMin(dic,int(v))
if (nextMinInx-i) <= k:
k -= 1
else:
res += v
del dic[v][0]
if k > 0:
res = res[:len(res)-k]
if len(res) == 0:
res = ''
return str(int(res))

【leetcode】402. Remove K Digits的更多相关文章

  1. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  2. 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 ...

  3. 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)

    [LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  4. 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)

    [LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...

  5. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

  6. 【LeetCode】423. Reconstruct Original Digits from English 解题报告(Python)

    [LeetCode]423. Reconstruct Original Digits from English 解题报告(Python) 标签: LeetCode 题目地址:https://leetc ...

  7. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

  8. [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 ...

  9. leetcode 402. Remove K Digits

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

随机推荐

  1. springSecurity总结

    springSecurity总结: 一.Spring security框架简介   1.简介           一个能够为基于Spring的企业应用系统提供声明式的安全訪问控制解决方式的安全框架(简 ...

  2. react 类样式的一些使用方法

    在 css类不想使用穿透的状态,可以再webpack配置  modules:true,它位于css-loader下,此外调用css的类时可能会自动生成一个hash值,这时候如果想显示本来的名字,可以打 ...

  3. be of +名词 = 形容词 (词性变化) ; || it is adj. of + 人称宾格 = 人称主格 + 形容词 (人称变化)

    be of +名词 = 形容词 这是一种常用的构词法 of + 名词 就等于 对应的形容词, 这也是, 扩展词汇的一种方法. 原则上你可以任意使用, 但是, 通常只是针对 那些形容词, 名词相对来说比 ...

  4. if else 更优雅的写法(转)

    https://www.cnblogs.com/y896926473/articles/9675819.html

  5. JS-在本页面禁止页面返回

    这个问题是最近遇到的 解决方案我百度的并测试有效 // 如果你希望用户不用有返回功能 可缩写如下 或使用location.replace('url')跳转链接 history.pushState(nu ...

  6. SpringBoot整合Lintener

    1.通过扫描完成Lintener组件的注册 1.1编写Listener /** * springboot整合Lintener 方式一 * 在web.xml中如何配置Listener * <lis ...

  7. C#实体类生成Create Table SQL

    using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespac ...

  8. python 正则表达式 re.split

    内置函数split与re库中的split,有很多相似处 #!use/bin/python #coding:utf-8 import re str= "https://i.cnb1logs.c ...

  9. vue中的computed 与 watch

    计算属性 computed 指通过计算得来的属性,用于监听属性的变化 computed里面的函数调用的时候 不需要加() 方法里必须有一个返回值 return computed中的函数不会通过事件去触 ...

  10. java Iterator Iterable Collection AbstractCollection Map关系

    java.lang Interface Iterable<T>  实现该接口就可以使用for-each循环. java.util Interface Iterator<E>   ...