leetcode 387. First Unique Character in a String(字符串中第一个独一无二的字母)

LeetCode --- 387. First Unique Character in a String解题报告 Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note:You ... 阅读详情

Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

Example 1:

Input: s = “leetcode”
Output: 0
Example 2:

Input: s = “loveleetcode”
Output: 2
Example 3:

Input: s = “aabb”
Output: -1

找出字符串s 中只出现一次的字母,返回它的index。
如果没有,返回-1。

思路:

网上有个1ms的解法,用到了字符串自带的indexOf, lastIndexOf函数,
如果它俩相等,证明只出现了一次,更新最小的index即可。

public int firstUniqChar(String s) {
    int result=s.length();
    
    for(char c='a';c<='z';c++){
        int firstIndex=s.indexOf(c);
        if(firstIndex!=-1 && firstIndex==s.lastIndexOf(c)){
            result=Math.min(result, firstIndex);
        }
    }
    
    return result==s.length()?-1:result;
}

这里我们不用字符串自带的函数,
因为字母只有26个,所以用一个数组记录下每个字母出现的次数,
然后找到次数为1的字母,它出现的最小index即可,

找最小index岂不是又要把s 遍历一次?no,
还是用一个数组记录下每个字母出现的最小index,
直接找到对应的index, 然后更新最小的index即可。

public int firstUniqChar(String s) {
    int[] cnt = new int[26];
    int[] firstIdx = new int[26];
    int res = s.length();
        
    Arrays.fill(firstIdx, -1);
    
    for(int i = 0; i < s.length(); i++) {
        int idx = s.charAt(i) - 'a';
        cnt[idx] ++;
        if(firstIdx[idx] == -1) firstIdx[idx] = i;
    }
    
    for(int i = 0; i < 26; i++) {
        if(cnt[i] == 1) res = Math.min(res, firstIdx[i]);
    }
    
    return (res == s.length() ? -1 : res);
}
【全网首发】2026华为OD双机位C卷 机考真题题库含考点说明以及在线OJ (Java) 如果你收到的是双机位C卷考试通知,需要针对性地调整备考策略。自2025年11月19日起,华为OD机试已正式从双机位A卷切换至。复杂度明显提升,需要组合多个算法点。题面相对简单,直接考查编程基本功。考查问题分析、算法选择和优化能力。通常为1-2个核心算法点的应用。 阅读详情

相关推荐

yanshee 机器人全流程指南

2.点击yanshee app右上角根据提示操作 3.当手机显示成功连接 ,按下机器人胸前按钮说出“ip地址”4.根据机器人播报的ip地址加上:8888使用浏览器访问,你就会看到 jupyterd的界面,里面可以新建文件也可以改他存在的文件。我们使了yanshee机器人,这个是面向K12、中高职、高校学生推出的开源人形机器人教育平台,应用于课程教学、竞赛、科研活动等场景。我们接下来可以根据这些apl做许多好玩的,比如控制他运动,跳舞唱歌,it`s easy。注意电脑,手机,机器人必须连同一个网。

qq_44827929的博客 7610

LeetCode387—First Unique Character in a String

LeetCode387 First Unique Character in A String

上善若水 1865

第10章:位同步与Gardner环路(1)

第10章(1)内容如下:一、位同步与载波同步的区别1、采样时钟相位偏差的影响2、采样时钟频率偏差的影响二、Gardner环路算法与插值方法1、插值滤波器2、Gardner误差检查算法三、Gardner环路中环路滤波器与数字振荡器设计1、环路滤波器2、数控振荡器(NCO)四、Gardner环路的可运行MATLAB代码五、参考资料六、致谢 最近搞懂了Gardner环路的一些知识,于是更新第10章——位同步。 如果你稍不留意的话,这可能是你在网上能看到的介绍Gardner环路及其MATLAB仿真最详细的一篇文章了

weixin_44283855的博客 1万+

leetcode 387. First Unique Character in a String

目录 一、问题描述 二、代码实现 1、两遍遍历 2、一遍遍历 https://leetcode.com/problems/first-unique-character-in-a-string/ 返回给定字符串第一个只出现一次的字符的下标,不存在则返回-1 一、问题描述 测试用例: s = "leetcode" return 0. s = "loveleetcode...

小景的博客 215

Word毕业论文排版:图片和表格题注的终极指南(含自动编号技巧)

本文是Word毕业论文排版中图片和表格题注的终极指南,详细讲解了如何利用题注功能实现自动编号,告别繁琐易错的手动操作。核心内容包括创建定制标签、配置带章节的编号格式、插入交叉引用以及一键更新全文编号的技巧,旨在帮助学生构建自动化文档体系,提升排版效率和论文严谨性。

weixin_29211309的博客 1093

24. 在QML界面中嵌入QWidget窗口

目前QT官方主推的界面开发方式是采用QML进行界面设计,但在QML未流行之前,很多的项目都是采用QWidget开发的,把之前的代码全部转换为QML代码显然工作量非常大,如果能将QWidget窗口嵌入到QML界面中,那么开发效率将提高很多,且QML无法实现的功能也能借助于QWidget框架进行实现。

山间点烟雨 5921

387. First Unique Character in a String*

387. First Unique Character in a String* https://leetcode.com/problems/first-unique-character-in-a-string/description/ 题目描述 Given a string, find the first non-repeating character in it and return it’s...

珍妮的选择的博客 263

无法加载文件:C:\Users\Lenovo\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本编程

当您遇到类似于"无法加载文件:C:\Users\Lenovo\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本编程"的错误消息时,请按照上述步骤来解决问题。当您尝试加载文件C:\Users\Lenovo\AppData\Roaming\npm\cnpm.ps1时,可能会遇到类似于"无法加载文件:C:\Users\Lenovo\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本编程"的错误消息。请注意,更改执行策略可能会带来一些安全风险。

PixelInk的博客 1919

Leetcode PHP题解--D100 387. First Unique Character in a String

D100 387. First Unique Character in a String 题目链接 387. First Unique Character in a String 题目分析 返回给定字符串中第一个只出现了一次的单词下标。 若没有,则返回-1。 思路 把遇到的单词存进两个数组。 ...

244

在ubuntu20.04中安装MATLAB时常见问题及解决方法

   如果你刚装完ubuntu20.04,这时你想要安装MATLAB2021A,在输入命令./install进行安装时,可能会报以下错误:    terminate called after throwing an instance of ‘JailContainerException’ what(): Error: 88: No JRE version parsed from/media/jzx/MATHWORKS_R2021A/sys /java/jre/glnxa64/jre/release   

慕羽★的博客 1万+

高通IPA硬件加速介绍

‌ 高通IPA (Internet Protocol Accelerator)模块是一种硬件加速模块,由高通公司(Qualcomm)提供,主要用于优化移动设备上的网络数据传输,特别是通过 LTE 、 5G 等移动网络的数据传输‌‌。IPA模块通过硬件加速,显著提高了数据传输的速度和效率,同时降低了设备的功耗,并提供了更稳定的网络连接,减少了数据传输中的丢包和延迟‌。

四儿家的小祖宗的博客 1622

【LeetCode】387. First Unique Character in a String 字符串中的第一个唯一字符(Easy)(JAVA)

【LeetCode】387. First Unique Character in a String 字符串中的第一个唯一字符(Easy)(JAVA) 题目地址: https://leetcode.com/problems/first-unique-character-in-a-string/ 题目描述: Given a string, find the first non-repeating character in it and return its index. If it doesn’t exist,

qq_16927853的博客 183

Rerender A Video 技术浅析(四):跨帧约束

时间一致性是指在视频处理过程中,保持相邻帧之间内容、风格和运动的平滑过渡,避免出现视觉上的不连续性或伪影。时间一致性跨帧约束模块的主要目标是在视频增强、风格迁移等任务中,确保处理后的视频在时间维度上保持一致性和稳定性。该模块主要通过以下几种方式实现时间一致性:1.时序损失(Temporal Loss):最小化相邻帧之间的差异,确保帧间内容的平滑过渡。2.循环一致性损失(Cycle Consistency Loss):确保帧间转换的一致性,避免出现风格漂移。3.

m0_75253143的博客 1480

LeetCode 387. 字符串中的第一个唯一字符(First Unique Character in a String)

387. 字符串中的第一个唯一字符 LeetCode387. First Unique Character in a String 题目描述 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. 注意事项:您可以假定该字符串只包含小写字母。 Java 实现 cla...

weixin_30830327的博客 263

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

leetcode 387. First Unique Character in a String 详解 python3

一.问题描述 Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. No...

CSerwangjun的博客 424

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

leetcode[387]First Unique Character in a String

问题:给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。 输入:一个字符串(仅包含英文小写字母) 输出:第一个不重复的字符索引(不存在则返回-1) 思路: 遍历一遍字符串,利用hash表存储,每个字符为key时,value加1。 然后,正序遍历一遍字符串,如果value为1,则返回其索引。 (或者,遍历一遍hash表,如下第一个代码,判断是否为不重复的数字,比较得到最小的索...

Black Mamba的博客 190

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

LeetCode String 387 First Unique Character in a String

387. First Unique Character in a String Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1. Examples: s = “leetcode” return 0. s = “lov...

Yi Jiao's Blog 141

【LeetCode】387. First Unique Character in a String 字符串中的第一个唯一字符(Easy)(JAVA)每日一题

【LeetCode】387. First Unique Character in a String 字符串中的第一个唯一字符(Easy)(JAVA) 题目地址: https://leetcode-cn.com/problems/first-unique-character-in-a-string/ 题目描述: Given a string, find the first non-repeating character in it and return its index. If it doesn’t exi

qq_16927853的博客 220

LeetCode 387 First Unique Character in a String 字符串中的第一个唯一的字母 C语言

387. First Unique Character in a String Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1. . 从给定的字符串中找到第一个不重复的字母,并输出其在字符串中的指针位置(第一个字母的位...

高能粒子的博客 223

LeetCode笔记:387. First Unique Character in a String

找到一个字符串中第一个只出现一次的字母

Minecraft 2363

LeetCode 387. First Unique Character in a String 题解(C++)

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.

prayyojay的博客 468

02.leetcode-387.First Unique Character in a String

题目: 387.字符串中的第一个唯一字符Given a string, find the first non-repeating character in it and return its index. If it doesn't exist, return -1. 找出字符串中的第一个唯一(不重复)字符;不存子啊返回-1 分析: 暴力破解时间复杂度依旧为O(n^2); 此外还有许多其他方法,这里就不介绍了,包括hash; 解决方案: 循环26个字母,统计每个字母出现次数为1的字母.

ScorpC的博客 273

LeetCode 387. First Unique Character in a String

题目: 给定一个字符串,找到其中第一个非重复字符并返回它的索引。如果不存在,返回-1。 Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. s = "leetcode" return 0. s = "l...

冲鸭!!! 166

LeetCode - 387. First Unique Character in a String

链接 387. First Unique Character in a String 题意 给定一个字符串(只有小写字母),返回其中第一个没有重复的字母的下标 思路 把这些字母转成数字存入数组,值为出现的次数。按照字符串的字母顺序遍历,当值为1时返回即可。 代码 Java : public class Solution { public int firstUniqChar(Str...

157

Leetcode|387. First Unique Character in a String字符串中的第一个唯一字符.java

题目 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。 示例 : s = “leetcode” 返回 0 s = “loveleetcode” 返回 2 提示: 你可以假定该字符串只包含小写字母。 标签 字符串;哈希表 解题思路 方法:哈希表 先将每个字符出现的次数统计到哈希映射中,再进行第二次遍历,找出只出现一次的字符,返回其索引即可。 代码 public class Leetcode387_FirstUniqueCharacterIn_a_String {

梦晨涌京 128

java字符串hash唯一_LeetCode 387: 字符串中的第一个唯一字符 First Unique Character in a String...

LeetCode 387: 字符串中的第一个唯一字符 First Unique Character in a String题目:给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。Given a string, find the first non-repeating character in it and return it's index. If it...

weixin_42165490的博客 255
上一篇: ESANet语义分割与realsense D455的结合
下一篇: leetcode 804. Unique Morse Code Words(独特的摩斯码单词)
蓝羽飞鸟
博客等级 码龄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、付费专栏及课程。

余额充值