当前位置:网站首页>LeetCode_字符串_简单_387. 字符串中的第一个唯一字符
LeetCode_字符串_简单_387. 字符串中的第一个唯一字符
2022-06-21 16:07:00 【一瓢江湖我沉浮】
1.题目
给定一个字符串 s ,找到 它的第一个不重复的字符,并返回它的索引 。如果不存在,则返回 -1 。
示例 1:
输入: s = “leetcode”
输出: 0
示例 2:
输入: s = “loveleetcode”
输出: 2
示例 3:
输入: s = “aabb”
输出: -1
提示:
1 <= s.length <= 105
s 只包含小写字母
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/first-unique-character-in-a-string
2.思路
(1)哈希表
(2)数组计数
3.代码实现(Java)
//思路1————哈希表
class Solution {
public int firstUniqChar(String s) {
//hashmap用于存储字符串 s 中每一种字符出现的次数
Map<Character, Integer> hashmap = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
hashmap.put(c, hashmap.getOrDefault(c, 0) + 1);
}
//找出 hashmap 中出现次数为 1 的字符,并返回其下标
for (int i = 0; i < s.length(); i++) {
if (hashmap.get(s.charAt(i)) == 1) {
return i;
}
}
//不存在唯一字符,返回 -1
return -1;
}
}
//思路2————数组计数
class Solution {
public int firstUniqChar(String s) {
/* 字符串 s 中只包含小写字母,故使用长度为 26 的数组 freq 来存储每个字符在 s 中出现的次数 freq[0]存储 a 出现的次数,freq[1]存储 b 出现的次数,以此类推。 */
int[] freq = new int[26];
int length = s.length();
for (int i = 0; i < length; i++) {
freq[s.charAt(i) - 'a']++;
}
for (int i = 0; i < length; i++) {
//如果找到只出现一次的字符,直接返回其下标即可
if (freq[s.charAt(i) - 'a'] == 1) {
return i;
}
}
//不存在唯一字符,返回 -1
return -1;
}
}
边栏推荐
- Leetcode 25: a group of K flipped linked lists
- Vector data download for mainland and global epidemic data, based on geo JSON to SHP
- Volcano engine + Yanrong yrcloudfile, driving new growth of data storage
- Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales bull?
- clickhouse学习笔记2:基本使用教程
- xlrd寻找指定内容所在行与行内容
- Accélérer le déploiement de l'application Native Cloud et compléter l'authentification de compatibilité entre Yanrong yrcloudfile et Tianyi Cloud
- 大话内存四区
- Kotlin DSL build
- BM95 分糖果问题
猜你喜欢

Accélérer le déploiement de l'application Native Cloud et compléter l'authentification de compatibilité entre Yanrong yrcloudfile et Tianyi Cloud

Kubernetes + 焱融 SaaS 数据服务平台,个性化需求支持就没输过

Common formula of derivative__ Common formulas of indefinite integral

The next stop of Intelligent Manufacturing: cloud native + edge computing two wheel drive
![[MySQL learning notes 18] constraints](/img/29/c72f83bfae8fd8b43e78cdf1aa9cbc.png)
[MySQL learning notes 18] constraints

软件测试体系学习及构建(14)-测试基础之软件测试和开发模型概述

「运维有小邓」Active Directory批量修改用户

软件测试体系学习及构建(13)-测试基础之测试工程师的基本要求

变量与指针

MySQL 1055错误-this is incompatible with sql_mode=only_full_group_by解决方案
随机推荐
软件测试体系学习及构建(14)-测试基础之软件测试和开发模型概述
大话内存四区
拉格朗日插值
Four areas of telephone memory
叩富网开期货账户安全可信吗?
Stack growth direction and memory growth direction
如何写好技术文档 Software Engineering at Google
Summary of the 16th week
窗帘做EN 1101易燃性测试过程是怎么样的?
Implementation of decode function in GP
The node server res.end() writes Chinese, and the solution to the problem of garbled code in the client
Niuke.com: large number addition
Jetpack Compose 的阶段
AttributeError: module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipeline‘
《MATLAB 神经网络43个案例分析》:第27章 LVQ神经网络的预测——人脸朝向识别
module.exports指向问题
Matlab中xticks函数
常见设置模式
硅橡胶玻纤管EN45545防火试验的难易程度
Kotlin DSL构建