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 = & ...
随机推荐
- ORA-27090: Unable to reserve kernel resources for asynchronous disk I/O
2019-08-19T09:27:33.225584+08:00Slave encountered ORA-27090 exception during crash recoveryRecovery ...
- Configuration on demand is not supported by the current version of the Android Gradle plugin since you are using Gradle version 4.6 or above. Suggestion: disable configuration on demand by setting org
androidStudio打开cocos3.17.2Lua项目时,出现了 Configuration on demand is not supported by the current version ...
- 原创【cocos2d-x】CCMenuItemToggle 在lua中的使用
说明:1,所使用的cocos2dx版本为2.1.3 ;09:48:05 2,本人仍是在学习中的小菜鸟,此博客只是为了记录我学习过程中的点滴,同时也希望同样lua开发的童鞋,一起交流: 3,本人whj0 ...
- IT兄弟连 HTML5教程 HTML5表单 小结及习题
小结 HTML表单提交的方法有get方法和post方法,get方法的作用是从指定的资源请求数据,post方法的作用是向指定的资源提交要被处理的数据.HTML表单一直都是Web的核心技术之一,有了它我们 ...
- Microsemi Libero系列教程(一)——Libero开发环境介绍、下载、安装与注册
前言 相比与Xilinx和Altera在国内的市场,Microsemi的FPGA在国内应用很少很少,网上几乎没有详细的教程,刚开始使用时,遇到了各种问题,自己也走了不少弯路.本系列教程以Libero ...
- Vue 从入门到进阶之路(十一)
之前的文章我们说了一下 vue 中组件的原生事件绑定,本章我们来所以下 vue 中的插槽使用. <!DOCTYPE html> <html lang="en"&g ...
- MVC过滤器:自定义操作过滤器
一.操作过滤器 1.定义 操作过滤器用于实现IActionFilter接口以及包装操作方法执行.IActionFilter接口声明两个方法:OnActionExecuting和OnActionExec ...
- SimpleDateFormat类简单学习
一.简介 SimpleDateFormat是一个格式化和解析日期的具体类,其可以将时间转化为指定格式的日期字符串,也可以将具有格式的日期字符串转换为时间. formatting (date → tex ...
- Access Grid Control Properties 访问网格控件属性
In this lesson, you will learn how to access the properties of a list form's Grid Control in WinForm ...
- Reuse Implemented Functionality 重用实现功能
A default XAF solution contains one platform-agnostic (shared) module and platform-dependent modules ...