当前位置:网站首页>#yyds干货盘点# 解决剑指offer:字符流中第一个不重复的字符
#yyds干货盘点# 解决剑指offer:字符流中第一个不重复的字符
2022-06-12 21:39:00 【51CTO】
1.简述:
描述
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符 "go" 时,第一个只出现一次的字符是 "g" 。当从该字符流中读出前六个字符 “google" 时,第一个只出现一次的字符是"l"。
数据范围:字符串长度满足 ,字符串中出现的字符一定在 ASCII 码内。
进阶:空间复杂度 ,时间复杂度
后台会用以下方式调用 Insert 和 FirstAppearingOnce 函数
string caseout = "";1.读入测试用例字符串casein2.如果对应语言有Init()函数的话,执行Init() 函数3.循环遍历字符串里的每一个字符ch {Insert(ch);caseout += FirstAppearingOnce()}2. 输出caseout,进行比较。
返回值描述:
如果当前字符流没有存在出现一次的字符,返回#字符。
示例1
输入:
复制返回值:
复制
示例2
输入:
返回值:
2.代码实现:
import java.util.*;
public class Solution {
private StringBuilder s = new StringBuilder();
private HashMap<Character, Integer> mp = new HashMap<>();
//Insert one char from stringstream
public void Insert(char ch)
{
//插入字符
s.append(ch);
//哈希表记录字符出现次数
mp.put(ch, mp.getOrDefault(ch, 0) + 1);
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce()
{
//遍历字符串
for(int i = 0; i < s.length(); i++)
//找到第一个出现次数为1的
if(mp.get(s.charAt(i)) == 1)
return s.charAt(i);
//没有找到
return '#';
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
边栏推荐
- Principales étapes de la collecte des ordures à Zgc
- Icml2022 | Galaxy: apprentissage actif des cartes de polarisation
- 选择排序
- Recommended Chinese font in the code input box of Oracle SQL developer
- JVisualVM初步使用
- Bubble sort
- VagrantBox重新安装vboxsf驱动
- 服务没有报告任何错误mysql
- 同花顺能开户吗,在APP上可以直接开通券商安全吗 ,证券开户怎么开户流程
- JUC并发工具包使用指南
猜你喜欢

@loadbalance annotation of resttemplate

ICML2022 | GALAXY:极化图主动学习
![[target detection] |dive detector into box for object detection new training method based on fcos](/img/ac/c54c2733dceffea086b772f35f128a.png)
[target detection] |dive detector into box for object detection new training method based on fcos

利用ADG Standby克隆PDB

MySQL master-slave replication

Pixel level reconstruction and restoration technology to solve severe image blur

Kdd2022 | graphmae: self supervised mask map self encoder

Npoi create word

A high-value MySQL management tool

Smart management of green agriculture: a visual platform for agricultural product scheduling
随机推荐
[target detection] |dive detector into box for object detection new training method based on fcos
Oracle数据库中查询执行计划的权限
Can tonghuashun open an account? Is it safe to open an account in tonghuashun
The ifeq, filter and strip of makefile are easy to use
NPOI 创建Word
SQL调优指南笔记10:Optimizer Statistics Concepts
Bubble sort
Experiment 7-2-6 print Yanghui triangle (20 points)
#886 Possible Bipartition
冒泡排序
Ubuntu16.04 completely delete MySQL database
jsonUtils
#886 Possible Bipartition
好数对的求解
Icml2022 | galaxy: active learning of polarization map
Is it safe to open an account in flush? How to open an account online to buy stocks
实现从字符串中删除某个字符操作
测试基础之:单元测试
风控建模十:传统建模方法存在的问题探讨及改进方法探索
SQL调优指南笔记8:Optimizer Access Paths