博客
关于我
【Lintcode】244. Delete Char
阅读量:224 次
发布时间:2019-02-28

本文共 950 字,大约阅读时间需要 3 分钟。

题目地址:

给定一个小写英文字母组成的字符串,再给定一个数字 k k k,求其最小字典序的长度为 k k k的子序列。

这个题目的思路与完全一样,只需要把一个字符串看成是 27 27 27进制的整数即可,然后同样用单调栈来做。代码如下:

public class Solution {       /**     * @param str: the string     * @param k: the length     * @return: the substring with  the smallest lexicographic order     */    public String deleteChar(String str, int k) {           // Write your code here.        if (k == 0) {               return "";        }                k = str.length() - k;        StringBuilder sb = new StringBuilder();        for (int i = 0; i < str.length(); i++) {               char c = str.charAt(i);            while (sb.length() > 0 && sb.charAt(sb.length() - 1) > c && k > 0) {                   sb.setLength(sb.length() - 1);                k--;            }                        sb.append(c);        }                if (k > 0) {               sb.setLength(sb.length() - k);        }                return sb.toString();    }}

时空复杂度 O ( n ) O(n) O(n)

转载地址:http://dhds.baihongyu.com/

你可能感兴趣的文章
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中null和空字符串的区别与问题!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
Mysql中存储引擎简介、修改、查询、选择
查看>>
mysql中实现rownum,对结果进行排序
查看>>
mysql中对于数据库的基本操作
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
MySQL中的count函数
查看>>
MySQL中的DB、DBMS、SQL
查看>>
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>