C#LeetCode刷题之#387-字符串中的第一个唯一字符(First Unique Character in a String)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3939 访问。
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
s = "leetcode",返回 0.
s = "loveleetcode",返回 2.
注意事项:您可以假定该字符串只包含小写字母。
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 = "loveleetcode",return 2.
Note: You may assume the string contain only lowercase letters.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3939 访问。
public class Program {
public static void Main(string[] args) {
var s = "loveleetcode";
var res = FirstUniqChar(s);
Console.WriteLine(res);
Console.ReadKey();
}
private static int FirstUniqChar(string s) {
var dic = new Dictionary<int, int>();
for(var i = 0; i < s.Length; i++) {
if(dic.ContainsKey(s[i])) {
dic[s[i]]++;
} else {
dic[s[i]] = 1;
}
}
for(var i = 0; i < s.Length; i++) {
if(dic[s[i]] == 1) return i;
}
return -1;
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3939 访问。
2
分析:
显而易见,以上算法的时间复杂度为: 。
C#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 charact ...
- [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/ 给定一个字符串,找到它的第 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- leecode刷题(13) -- 字符串中的第一个唯一字符
leecode刷题(13) -- 字符串中的第一个唯一字符 字符串中的第一个唯一字符 描述: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = & ...
- LeetCode初级算法--字符串02:字符串中的第一个唯一字符
LeetCode初级算法--字符串02:字符串中的第一个唯一字符 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog. ...
- LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)
题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...
- 字符串中的第一个唯一字符 python
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. s = "leetcode" 返回 0. s = "loveleetcode&qu ...
随机推荐
- css导航菜单二级显示的问题
m项目中出现了二级菜单的标签是在导航的里面,用css ul>li:hover ul>li>ul>li 这样子实现不了鼠标经过时导航里二级菜单的显示,这里个人感觉是冲突了.最后通 ...
- GPO - General GPO Settings(3)
WMI filtering Setting - Differentiating Installation Between Operations and Architecture. WMI SQL Ge ...
- CUDA Programming Guide 学习笔记
CUDA学习笔记 GPU架构 GPU围绕流式多处理器(SM)的可扩展阵列搭建,每个GPU有多个SM,每个SM支持数百个线程并发执行.目前Nvidia推出了6种GPU架构(按时间顺序,详见下图):Fer ...
- IDEA添加注释常用的快捷键
1.行注释Ctrl+/ 2.块注释Ctrl+Shift+/ 3.生成类注释 输入/**,然后按回车 (idea上没有生成类注释快捷键的,可以看这里 :idea生成类注释和方法注释的正确方法 ) 4.生 ...
- Oracle可视化工具连接
Oracle可是化工具有很多,以下只列举sql developer和sql plus这两款连接方式 sql developer: SQL Develope启动后,需要创建一个数据库连接,只有创建了数据 ...
- SOLID:面向对象设计的前五项原则
S.O.L.I.D是Robert C. Martin提出的前五个面向对象设计(OOD)原则的首字母缩写,他更为人所熟知的名字是Uncle Bob. 将这些原理结合在一起,可使程序员轻松开发易于维护 ...
- flask中的endpoint是什么
app.view_functions 是一个字典,里面是存储的是 endpoint 与 视图函数的键值对,如果没指名函数视图的endpoint,默认是函数名,而 url_map 是一个列表,里面是ur ...
- Csrf+Xss组合拳
本文首发于“合天智汇”公众号,作者: 影子 各位大师傅,第一次在合天发文章,请多多关照 今年年初的疫情确实有点突然,打乱了上半年的所有计划(本来是校内大佬带我拿奖的时刻,没了 ,学长毕业了,就剩下我这 ...
- hashlib加密算法
# import hashlib # mima = hashlib.md5()#创建hash对象,md5是信息摘要算法,生成128位密文 # print(mima) # # mima.update(' ...
- MySQL在同一个表上,删除查询出来的结果
背景 有一个程序员员工表(code_user),包含用户id.姓名.掌握的语言. 表数据如下: +---------+-----------+----------+ | user_id | user_ ...