leetcode 316. Remove Duplicate Letters(删除重复的字母)

idea 编译打包nacos2.0.3源码,生成可执行jar 包常见问题 阅读详情

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.

Example 1:

Input: “bcabc”
Output: “abc”
Example 2:

Input: “cbacdcbc”
Output: “acdb”

给出一个字符串,让output出unique的字符,
而且字符的顺序:比如上例的“bcabc",当前面和后面都有bc的时候,因为a在字符顺序上位于bc前面,所以只输出后面的bc,unique输出是abc
第二个例子,c的字母顺序在d前面,尽管后面有c,也是输出cd,全体是acdb

思路:
先读一遍字符串,记下每个字符出现了几次,记在count数组中。
字母只有26个,直接用数组即可,不需要HashMap。

然后再读一遍,这次要把字母保存到result字符串中。同时用过的字母它对应的次数要-1。
我们要保证result中字母是排好序的,怎么做到呢?就是每次来一个新字母的时候都要把前面比它大的删掉,当然如果前面比它大的仅此一个(用到了出现次数),后面没了,就不能删了。

如果result为空,直接把字母append进去,同时用一个visited数组标记这个字母已经在result里面了,如果下次s中出现了同样的,就不需要再处理了,直接跳过。

因为result中是已经按要求排好序的,所以不必纠结每次都要遍历一遍result,都要去和当前s的字母比较,只需要把result最后的字母(最大的)和s当前字母比,如果比s[i]小,就直接append s[i],不需再比。

如果result最后的字母比s[i]大,是不是就要删除?不一定,因为如果后面没有这个字母了怎么办,不就少一个字母了。
这时count数组起作用了,它记录了每个字母出现的次数,如果次数为0了,说明后面没有了,不能删。

如果后面还有,那么删掉,再取 下一个result的最后字母,直到不满足条件或result为空为止。

    public String removeDuplicateLetters(String s) {
        int[] cnt = new int[26];
        int n = s.length();
        boolean[] visited = new boolean[26];
        StringBuilder result = new StringBuilder();
        
        for(int i = 0; i < n; i ++) {
            cnt[s.charAt(i) - 'a'] ++;
        }
        
        for(int i = 0; i < n; i ++) {
            int len = result.length();
            char sc = s.charAt(i);
            
            cnt[sc - 'a'] --;  //用过字母的次数-1
            
            if(visited[sc - 'a']) continue;  //已经在result里面的不需要再处理
            
            while(len > 0 && result.charAt(len-1) > sc && 
                  cnt[result.charAt(len-1) - 'a'] > 0) {  
                visited[result.charAt(len-1) - 'a'] = false; //删掉了,result中没有了,要在删掉之前,因为result.length会变
                result.deleteCharAt(len - 1);
                len --;
            }
            result.append(sc);
            visited[sc - 'a'] = true;
        }
        
        return result.toString();
    }
金蝶云星空核心数据表解析与高效关联查询指南 本文深入解析金蝶云星空核心数据表结构,提供高效关联查询的实战指南。文章详细介绍了基础资料、业务单据和财务模块的关键表及其关联逻辑,重点阐述了如何利用关联表(_LK表)进行跨单据溯源,并分享了编写高性能SQL的实用技巧与避坑建议,帮助开发者、实施顾问和数据分析师快速掌握数据查询方法,提升工作效率。 阅读详情

相关推荐

TexGen模型导出到ABAQUS的inp文件:如何快速修改纱线材料属性(避坑指南)

本文针对TexGen模型导出到ABAQUS的inp文件后纱线材料属性缺失或错误的问题,提供了高效的解决方案。文章详细解析了inp文件结构,指导用户如何手动定位并修改纱线材料属性,并提供了可批量处理的Python脚本,帮助用户快速、精准地完成材料参数赋值,有效提升复合材料仿真工作效率。

ice55的博客 612

leetcode 316. Remove Duplicate Letters

题目 :Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: s = “bcabc” Output: “abc” Example 2: Input

zjshuster的博客 235

SAP物料分类账配置避坑指南:从货币类型到实际成本激活的5个关键步骤

本文深入解析SAP物料分类账配置中的5个关键步骤,涵盖货币类型选择、财务扩展激活、物料分类账类型设置等核心环节,帮助用户避开货币陷阱与成本激活的常见错误。特别针对业务配置中的货币体系架构和实际成本激活提供专业指导,确保企业成本数据准确性。

weixin_42561040的博客 403

【LeetCode】316. Remove Duplicate Letters 解题报告(Python & C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法日期 题目地址:https://leetcode.com/problems/remove-duplicate-letters/ 题目描述 Given a string which contains only lowercase letters, remove...

负雪明烛 3240

春秋云境-Apache Struts2 目录遍历及文件上传漏洞(CVE-2023-50164)

攻击者可以操纵文件上传参数以启用路径遍历,在某些情况下,这可能导致上传恶意文件,从而执行远程代码。建议用户升级到Struts 2.5.33或Struts 6.3.0.2或更高版本以解决这个问题。

shelbytt的博客 1125

【Python LeetCode 专题】面试经典 150 题

面试经典 150 题

一杯水果茶!足矣~ 1190

leetcode316: Remove Duplicate Letters 使用递归的简单java实现

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 a

通往花开的路上 1895

Altium Designer应用技巧1: 功能模块的复用方法

Altium Designer应用技巧1: 功能模块的复用方法      我们在使用Altium Designer进行电路图设计时,常会遇到功能模块的复用问题,例如我们经常需要将12V以DC-DC的方式转换成5V,这在很多线路板上都会应用到。现在我们普通的做法是,每个板子都重新绘制一遍,像这样DC-DC的电路,很浪费时间。那么如何采用提高工作效率呢?我们可以采取Altium Desi

snmplink的博客 1万+

LeetCode 316. Remove Duplicate Letters--贪心算法

题目链接 316. Remove Duplicate Letters 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

monkey_rose的博客 400

GEE:基于 Landsat 数据集生成(1986-2024)月度合成地表温度(LST)时间序列数据

CSDN@_养乐多_本文将介绍使用 Landsat 时间序列计算(1986-2024)月度合成地表温度(LST)时间序列数据集的代码。

养乐多的博客 1731

LeetCode 316. Remove Duplicate Letters(删除重复字母)

原题网址:https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You mus

James Pan 2357

从零开始:手把手教你设计人形机器人锂电池供电系统

人形机器人要像人类一样“活起来”,必须有稳定的“能量血液”——供电系统。本文聚焦锂电池供电系统如何根据机器人功耗选择合适的锂电池参数(容量、电压、放电倍率);如何设计电池管理系统(BMS)保护电池安全;如何解决机器人运动时的“瞬时大电流”供电难题;如何通过硬件电路和代码实现供电系统的监测与控制。用“机器人小助手的能量危机”故事引入锂电池供电系统;拆解锂电池核心参数(容量、电压、放电倍率)和供电系统三大组件(电池组、BMS、电源转换模块);

AI天才研究院 1770

LeetCode: 316. Remove Duplicate Letters

LeetCode: 316. Remove Duplicate Letters 题目描述 Given a string which contains only lowercase letters, remove duplicate letters so that every letter appears once and only once. You must make sure your res...

杨领well的专栏 279

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

leetcode 316 Remove Duplicate Letters 去除重复字母

描述 给定一个仅包含小写字母的字符串,去除字符串中重复的字母,使得每个字母只出现一次。需保证返回结果的字典序最小(要求不能打乱其他字符的相对位置)。 示例 1: 输入: “bcabc” 输出: “abc” 示例 2: 输入: “cbacdcbc” 输出: “acdb” 思路 遍历s中的每一个字符,因为我们的结果集的字典序需要最小,所以如果这个字符比结果集中最后一个字符小而且结果集的最后一个字符在输...

yrk0556的博客 290

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

[leetcode] 316. Remove Duplicate Letters 解题报告

题目链接: https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You mu

小榕流光的专栏 3176

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

leetcode 316 Remove Duplicate Letters

lc316 Remove Duplicate Letters 我们用一个栈来保存最终的返回结果, 现在来看看什么时候能删除一个元素,bcabc走到a的时候发现c大于a而且a后还有c,所以可以删除 由此可见,当array[i]之前的字母较大,且在i之后还有重复的字母,就能删除 如何实现这个逻辑呢?    1) 统计各个字母出现次数abc[26]   2) count[array[i]]-...

weixin_30548917的博客 128

leetcode - 316. Remove Duplicate Letters

算法系列博客之Greedy Greedy 贪心算法是一种非常优美的算法,不过贪心算法本身的可行性很多时候会受到一些局限。但是一旦能够找到一种可行的贪心策略,问题的解决将会变得非常高效,因为通常情况下,贪心算法的复杂度是O(n)本篇博客将运用这种思想来解决leetcode上316号问题 问题描述:Given a string which contains only lowercase letter

Quiteen的博客 581

leetcode:316. Remove Duplicate Letters 题解

题目 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 ord

iterate7的博客 2244

LeetCode 316. Remove Duplicate Letters

Description 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 lexicograph...

weixin_34029680的博客 111

LeetCode String 316 Remove Duplicate Letters

316. Remove Duplicate Letters Given a string which contains only lowercase letters, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smal...

Yi Jiao's Blog 176

[leetcode] 316. Remove Duplicate Letters

Description Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Note: This question is the same as 1081: https://lee

Learning from the mistakes 195

【leetcode】316. Remove Duplicate Letters

题目如下: 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 lexicographica...

weixin_34384915的博客 71
上一篇: leetcode 122. Best Time to Buy and Sell Stock II
下一篇: leetcode 321. Create Maximum Number
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址