首页 > 其他 > 详细

不同字符的最小子序列

时间:2019-06-21 09:11:01      阅读:121      评论:0      收藏:0      [点我收藏+]

不同字符的最小子序列

class Solution {
    public String smallestSubsequence(String text) {
        Stack<Character> stack = new Stack<>();
        char[] cs = text.toCharArray();
        int len = cs.length;
        char temp = 0;
        String sub = null;
        for(int i=0;i<len;i++) {
            if(i==0) {
                stack.push(cs[i]);
            }else {
                if(!stack.contains(cs[i])) {
                    temp = stack.peek();
                    sub = text.substring(i+1);
                    while(cs[i]<temp&&sub.indexOf(temp)!=-1) {
                        stack.pop();
                        if(stack.empty()){
                            break;
                        }
                        temp = stack.peek();
                    }
                    stack.push(cs[i]);
                }
            }
        }
        int stackLen = stack.size();
        char[] strD = new char[stackLen];
        for(int i=0;i<stackLen;i++) {
            strD[stackLen-1-i]=stack.pop();
        }
        return new String(strD);
    }
}

 

不同字符的最小子序列

原文:https://www.cnblogs.com/erdanyang/p/11062294.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!