当前位置:网站首页>【剑指Offer】50. 第一个只出现一次的字符
【剑指Offer】50. 第一个只出现一次的字符
2022-06-28 22:45:00 【LuZhouShiLi】
剑指 Offer 50. 第一个只出现一次的字符
题目
在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
思路
新建一个哈希表,以键值对形式存储,值-每一个字符出现的次数(布尔类型),最后循环返回第一个只出现一次的字符。
代码
class Solution {
public char firstUniqChar(String s) {
HashMap<Character,Boolean> dic = new HashMap<>();
char[] sc = s.toCharArray();
for(char c : sc)
{
dic.put(c,!dic.containsKey(c));// 如果不在哈系表中 直接添加进去
}
for(char c:sc)
{
if(dic.get(c))
{
return c;// 获取第一次出现的字符
}
}
return ' ';// 没有的话返回空格
}
}
边栏推荐
- Mysql通过ibd文件恢复数据的详细步骤
- [SSH] login without password
- 超级工厂里的生意图鉴
- C#/VB. Net to convert PDF to excel
- Powerful open source API interface visual management platform Yapi
- Linux Installation mysql5.7 (centos7.6) tutorial
- 分享im即时通讯开发之WebSocket:概念、原理、易错常识
- Ingénieur natif du nuage après 00: utiliser Zadig pour développer des sources ouvertes et des économies d'énergie pour la technologie innovante (bus de Guangzhou)
- 复杂嵌套的对象池(4)——管理多类实例和多阶段实例的对象池
- Wave picking of WMS warehouse management system module
猜你喜欢

深入虚拟内存(Virtual Memory,VM)

Gross vs60 billion. Is food safety the biggest obstacle to Weilong's listing?

Jointly explore digital technology and information security, and the fourth China Russia Digital Forum was successfully held

QtCreator5.15.0源码编译全过程记录

WMS仓库管理系统模块之波次拣货

Detailed explanation of Zadig's self-test and joint debugging sub environment for developers

Lecun predicts AgI: big model and reinforcement learning are both ramps! My world model is the new way

Qtcreater5.15.0 source code compilation process record
Oracle set password complexity and timeout exit function

LeCun预言AGI:大模型和强化学习都是斜道!我的世界模型才是新路
随机推荐
深入虚拟内存(Virtual Memory,VM)
在长投学堂开通证券账户是安全可靠的吗?
In order to control the risks, how to choose a franchise company?
Oracle deleting archived logs and adding scheduled tasks
Explanation: Luogu p1762 even number /6.21 internal examination T2
CS5463代码模块解析(包含下载链接)
数据可视化中柱状图的实例应用,让乘风破浪公演结果一目了然
Pytorch builds transformer to realize multivariable and multi step time series forecasting (load forecasting)
Database basic notes
keil工程,程序写多后,RTT不能打印
Is it safe and reliable for changtou school to help open a securities account? How to drive
Yiming Anke submitted a statement to the Hong Kong Stock Exchange: the loss doubled in 2021, and the past financing amount was exaggerated
The new version of OpenAPI engine of Kingdee cloud dome is coming!
Qsrand, srand random number generating function in qt5.15 has been discarded
What does project management really manage?
After crossing, she said that the multiverse really exists
Considerations on the construction of operation and maintenance system - stability
Flowable boundary timer
Multiomics single cell data integration and regulatory reasoning based on graph linked embedding
Progress of dbnn experiment