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"
返回 0.
s = "loveleetcode",
返回 2.
注意事项:您可以假定该字符串只包含小写字母。
Note: You may assume the string contain only lowercase letters.
解题思路:
很简单的题,无非就是对字符串的字母进行频率统计,找到出现频率为1 的字母索引。
借助哈希映射两次遍历完成。第一次遍历进行字母频率统计,Hash Map 的Key 为字母,Value 为出现频率。第二次遍历找到频率为 1 的字母索引返回即可。
不同于单词频率统计,字母一共只有 26 个,所以可以直接利用 ASii 码表里小写字母数值从 97~122,直接用 int 型数组映射。建立映射:索引为 小写字母的 ASii 码值,存储值为出现频率。
哈希映射解题:
Java:
class Solution {
public int firstUniqChar(String s) {
char[] chars = s.toCharArray();//转成 Char 数组
Map<Character, Integer> map = new HashMap<>();
for (Character c: chars) map.put(c, map.getOrDefault(c, 0) + 1);//频率统计
for (int i = 0; i < chars.length; i++) {
if(map.get(chars[i])==1) return i;//找到词频为1的字母(只出现一次)返回其索引
}
return -1;
}
}
Python:
class Solution:
def firstUniqChar(self, s):
count = collections.Counter(s)# 该函数就是Python基础库里词频统计的集成函数
index = 0
for ch in s:
if count[ch] == 1:
return index
else:
index += 1
return -1
数组映射解题:
Java:
class Solution {
public int firstUniqChar(String s) {
char[] chars = s.toCharArray();
int base = 97;
int[] loc = new int[26];
for (char c:chars) loc[c - base] += 1;
for (int i = 0; i < chars.length; i++) {
if(loc[chars[i]-base]==1) return i;
}
return -1;
}
}
Python 基础数据结构里没有 char 型,强行使用chr(i)转换,只会导致效率更低
字符串函数解题:
Java:
利用 Java 字符串集成操作函数解题,很巧妙,效率也很高。
其中:
indexOf(): 返回该元素第一次出现的索引,没有则返回 -1
lastIndex(): 返回该元素最后一次出现的索引,没有则返回 -1
class Solution {
public int firstUniqChar(String s) {
int res = s.length();
for (int i = 'a'; i <= 'z'; i++) {
int firstIndex = s.indexOf((char)i);
if (firstIndex == -1) continue;
int lastIndex = s.lastIndexOf((char)i);
if (firstIndex == lastIndex) {//两次索引值相同则证明该字母只出现一次
res = Math.min(firstIndex, res);//res 为只出现一次的字母中索引值最小的
}
}
return res == s.length() ? -1 : res;
}
}
欢迎关注微信公众号: 爱写Bug

LeetCode 387: 字符串中的第一个唯一字符 First Unique Character in a String的更多相关文章
- [Swift]LeetCode387. 字符串中的第一个唯一字符 | 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 ex ...
- 前端与算法 leetcode 387. 字符串中的第一个唯一字符
目录 # 前端与算法 leetcode 387. 字符串中的第一个唯一字符 题目描述 概要 提示 解析 解法一:双循环 解法二:Set法单循环 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...
- Java实现 LeetCode 387 字符串中的第一个唯一字符
387. 字符串中的第一个唯一字符 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = ...
- LeetCode初级算法之字符串:387 字符串中的第一个唯一字符
字符串中的第一个唯一字符 题目地址:https://leetcode-cn.com/problems/first-unique-character-in-a-string/ 给定一个字符串,找到它的第 ...
- 【LeetCode】字符串中的第一个唯一字符
[问题]给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. s = "leetcode" 返回 . s = "loveleetcode ...
- 力扣(LeetCode)字符串中的第一个唯一字符 个人题解
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcod ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- LeetCode初级算法--字符串02:字符串中的第一个唯一字符
LeetCode初级算法--字符串02:字符串中的第一个唯一字符 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog. ...
- leecode刷题(13) -- 字符串中的第一个唯一字符
leecode刷题(13) -- 字符串中的第一个唯一字符 字符串中的第一个唯一字符 描述: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = & ...
随机推荐
- [转载]——Automatic Tuning of Undo_retention Causes Space Problems (文档 ID 420525.1)
Automatic Tuning of Undo_retention Causes Space Problems (文档 ID 420525.1) 转到底部 In this Document Sy ...
- 11G-使用跨平台增量备份减少可移动表空间的停机时间 XTTS (Doc ID 1389592.1)
11G - Reduce Transportable Tablespace Downtime using Cross Platform Incremental Backup (Doc ID 13895 ...
- c#中的跳转语句
break:跳出循环,执行循环外的语句:continue:跳出此次循环,进入下一次循环: goto:不建议使用 return:终止它所在的方法的执行,并将控制权返回给调用方法.
- Python学习笔记六(免费获取代理IP)
为获取网上免费代理IP,闲的无聊,整合了一下,免费从三个代理网站获取免费代理IP,目的是在某一代理网站被限制时,仍可从可以访问的其他网站上获取代理IP.亲测可用哦!^_^ 仅供大家参考,以下脚本可添 ...
- 5种智能指针指向数组的方法| 5 methods for c++ shared_ptr point to an array
本文首发于个人博客https://kezunlin.me/post/b82753fc/,欢迎阅读最新内容! 5 methods for c++ shared_ptr point to an array ...
- Java变量在内存中的存储
目录 Java变量在内存中的存储 成员变量 局部变量 总结 Java变量在内存中的存储 以下探究成员变量和局部变量在内存中的存储情况. package com.my.pac04; /** * @aut ...
- 在 Linux 下学习 C 语言有什么好处?
作者:宅学部落链接:https://www.zhihu.com/question/23893390/answer/832610610来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- Difference between JDK, JRE and JVM
With Java programming language, the three terms i.e. JDK, JRE and JVM will always be there to unders ...
- SpringMVC参数绑定,这篇就够了!
SpringMVC参数绑定,简单来说就是将客户端请求的key/value数据绑定到controller方法的形参上,然后就可以在controller中使用该参数了 下面通过5个常用的注解演示下如何进行 ...
- MySQL入门——Linux下安装后的配置文件
MySQL入门——Linux下安装后的配置文件 摘要:本文主要了解了在Linux环境下安装MySQL后的配置文件的位置,以及如何创建配置文件. 查看配置文件的加载顺序 找到mysqld的路径 通过wh ...