
316. Remove Duplicate Letters and 402. Remove K Digits
316 Remove Duplicate
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and
only once. You must make sure your result is the smallest in lexicographical order among all possible results.
Given "bcabc"
Return "abc"
Given "cbacdcbc"
Return "acdb"
每个字母只留一个,而且保证字典序最小。
从前往后扫描,还要往前看一个检出是否删除的,要用stack解。stack里记录的就是待用的字母。
首先要有一个counter记录下每个字母的总个数,stack删除字母的时候如果后面还有余量,则可以放心删除。
还有个关键词: once and only once。需要一个数据结构记录是否使用这个字母,可以用boolean。
public class Solution {
public String removeDuplicateLetters(String s) {
ArrayDeque<Character> stk = new ArrayDeque<Character>();
int[] counter = new int[26];
boolean[] visited = new boolean[26];
char[] chs = s.toCharArray();
for(char c: chs){
counter[c - ''a'']++;
}
for(char c : chs){
counter[c - ''a'']--;
if(visited[c - ''a'']) continue;
while(!stk.isEmpty() && stk.peekLast() > c && counter[stk.peekLast() -''a''] > 0){
visited[stk.peekLast() - ''a''] = false;
stk.pollLast();
}
stk.addLast(c);
visited[c-''a''] = true;
}
StringBuilder sb = new StringBuilder();
for(char c: stk){
sb.append(c);
}
return sb.toString();
}
}
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 possible
Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0.
stk结构也可以用数组加顶点指针模拟。用法如下。
public class Solution {
public String removeKdigits(String num, int k) {
int digit = num.length() - k;
char[] stk = new char[num.length()];
int top = 0;
for(char c: num.toCharArray()){
while(top > 0 && stk[top-1] > c && k > 0){
k--;
top--;
}
stk[top++] = c;
}
int idx = 0;
while(idx < digit && stk[idx] == ''0'') idx++;
return idx == digit ? "0" : new String(stk, idx, digit-idx);
}
}

centos – 如何撤消“yum remove python”
我有一个基于Centos 6的VPS与ssh连接.最近想要安装
python 2.7.2,我勇敢地跑了:
yum remove python
这显然删除了yum本身正在编写或取决于python.
现在我不知道该怎么办,更糟糕的是,我是一个完整的Linux新手(命令行或其他).
运行rpm -iv http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/python-2.6.5-3.el6.x86_64.rpm给出:
Retrieving http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/python-2.6.5-3.el6.x86_64.rpm
Preparing packages for installation...
package python-2.6.5-3.el6.x86_64 is already installed
它的x86_64服务器毫无疑问,因为“uname -a”给出:
Linux xxxxxx 2.6.32-71.29.1.el6.x86_64 #1 SMP Mon Jun 27 19:49:27 BST 2011 x86_64 x86_64 x86_64 GNU/Linux
还有一件事:ls /usr/bin/pyth * -la给出:
lrwxrwxrwx 1 root root 6 Dec 4 20:31 python2 -> python
-rwxr-xr-x 2 root root 4864 Nov 12 2010 python2.6
-rwxr-xr-x 2 root root 4864 Nov 12 2010 python;4edbd894
从
CentOS 6 mirrors列表中选择一个镜像,然后使用rpm安装所需的软件包.您需要处理依赖项.幸运的是,我有一个安装了CentOS 6 x86_64的裸机.
我快照VM,运行yum删除python并获得一些有趣的通过一些循环依赖.最后结果:
# --nodeps: because of circular dependency between python and python-libs
rpm -i --nodeps http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/python-libs-2.6.5-3.el6.x86_64.rpm
rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/python-2.6.5-3.el6.x86_64.rpm
rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/pygpgme-0.1-18.20090824bzr68.el6.x86_64.rpm
rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/python-iniparse-0.3.1-2.1.el6.noarch.rpm
rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/python-pycurl-7.19.0-5.el6.x86_64.rpm
rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/python-urlgrabber-3.9.1-7.el6.noarch.rpm
rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/rpm-python-4.8.0-12.el6.x86_64.rpm
rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/yum-Metadata-parser-1.1.2-14.1.el6.x86_64.rpm
# --nodeps: because of circular dependency between yum and yum-plugin-fastestmirror
rpm -i --nodeps http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.26-11.el6.noarch.rpm
rpm -i http://centos.mirrors.hoobly.com/6.0/os/x86_64/Packages/yum-3.2.27-14.el6.centos.noarch.rpm
更换任何你想要的镜子.此外,请确保架构匹配.那应该可以做到这一点!
哦,还有一件事:如果我是你,我会在你完成安装RPM后立即进行yum升级.

centos – 服务器在“yum remove python”之后擦除
我运行命令yum remove
python之后发生了一场灾难,现在我再也无法启动服务器了.
它是如何发生的:我尝试通过我的CentOS 5 VPS上的yum更新一些应用程序,并且命令因为一些奇怪的python 2.4错误而失败.我注意到我的python版本很旧,我尝试通过先删除它重新安装它,所以我做了yum删除python.
在那之后,它问了我一些关于删除依赖关系的事情,看起来我什么都不想错过,所以我点击了Y.
所以后果就是我无法执行任何命令.我甚至尝试过cd / var / www,但它说“/usr/bin中不存在命令”.当我使用tab查看文件夹导航建议时,文件结构似乎仍然存在(至少/ var / www位对我来说非常重要).
之后我尝试重新启动vps(从管理面板,因为重启命令不起作用),现在它不再启动了.
现在我的问题是:像这样的命令怎么可能像这样毁坏我的服务器?
我真诚地抱歉:让服务器无法启动/无法使用会让我感到痛苦.
但是,在阅读时我迷路了:
After that it asked me something about removing dependencies and it
looked like nothing I Could miss so I clicked [Y]
要删除的软件包列表肯定非常庞大,因为python是RHEL / CentOS的重要组成部分.你永远不应该确认一些你不太了解的警告信息.
正如已经建议的那样,你可以做的最好的事情是通过恢复媒体(即:livecd)启动,提取所需的数据文件,并使用更新的CentOS版本重新安装你的机器(并且因为CentOS 6很老,我强烈建议你在CentOS 7上进行改装.
关于python:remove,pop和del方法的区别和python中remove和pop的区别的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于301. Remove Invalid Parentheses 去掉不合理的括号、316. Remove Duplicate Letters and 402. Remove K Digits、centos – 如何撤消“yum remove python”、centos – 服务器在“yum remove python”之后擦除的相关信息,请在本站寻找。