题目如下:

解题思路:我的方法是从头开始遍历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. 架构-层-Model:Model

    ylbtech-架构-层-Model:Model 1.返回顶部 1. Model,意思是模特儿,模特儿是英文“model”的音译.模特一般来说要五官端正,身材良好,有气质,展示能力强,另外身高要具备一 ...

  2. 一些输出、处理细节&注意点

    https://blog.csdn.net/qq_41071646/article/details/79953476 输出百分比的时候,结果需要加上一个EPS(1e-6)四舍五入保证精度. 卡精度—— ...

  3. 新接口注册LED字符驱动设备

    #include <linux/init.h> // __init __exit #include <linux/module.h> // module_init module ...

  4. 构造函数与new的汇编实现

    this指针,通常是通过ecx传递:gcc是通过堆栈传递的,是最后一个被压栈.传递this指针是为了访问成员变量.除了虚函数,所有成员函数被编译之后都是全局函数.mov eax,[ecx] ; 将第一 ...

  5. centos解决Could not find a version that satisfies the requirement pip3 (from versions: none)及No matching distribution found for pip3问题

    python环境:python 3.8 报错信息: WARNING: pip is configured with locations that require TLS/SSL, however th ...

  6. ftp服务端

    #coding=utf-8 import SocketServer import json import os class MyTcpHandler(SocketServer.BaseRequestH ...

  7. nodejs回调大坑

    最近看到nodejs,因为有一个处理里面有好几个异步操作,调入回调大坑,不禁觉得很恶心,真的很讨厌发明这种写法的人,简直反社会!!!遂转载一篇解坑的文章,原文地址:http://www.infoq.c ...

  8. There is no Action mapped for namespace [/] and action name [TestAction] ass

    1.修改action的name值 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE strut ...

  9. 读取FTP上的excel文件,并写入数据库

    今天遇到一些问题,需要从ftp上读取一些excel文件,并需要将excel中的数据写入到数据库,这样就可以通过管理页面查看这些数据. 我将相关工作分为三步,1.从ftp上读取相关文件,并将excel文 ...

  10. Perl脚本通过Expect登陆多台设备批量执行命令并Log

    本例子尝试使用Perl脚本借助Expect模块实现如下目的: 登陆多台设备 设备登陆信息按如下格式存放于文件中. $ cat hosts.txt 192.168.30.7:node1:telnet:b ...