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)相关推荐
- Python——LeetCode刷题——【387. 字符串中的第一个唯一字符】
题目描述: 解题思路: 逐个遍历字符串中的每个字符.用字典记录在字符串中每个字符的出现次数.最后遍历字符串中每个字符,字典中该字符作为key对应的value值为1,return首个value值为1的字 ...
- leetcode 387. 字符串中的第一个唯一字符(First Unique Character in a String)
目录 题目描述: 示例: 解法: 题目描述: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 示例: s = "leetcode"返回 0. ...
- Leetcode 387. 字符串中的第一个唯一字符
387. 字符串中的第一个唯一字符 给定一个字符串 s ,找到 它的第一个不重复的字符,并返回它的索引 .如果不存在,则返回 -1 . 示例 1: 输入: s = "leetcode&quo ...
- 387. 字符串中的第一个唯一字符(javascript)387. First Unique Character in a String
leetcode:https://leetcode-cn.com/problems/first-unique-character-in-a-string/ 387. 字符串中的第一个唯一字符 给定一个 ...
- 387. 字符串中的第一个唯一字符
387. 字符串中的第一个唯一字符--2020.12.27 题目 思路 题目 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 示例: s = "lee ...
- LeetCode:387.字符串中的第一个唯一字符
字符串中的唯一字符就是字符出现次数为1的 思路:首先一次遍历,统计出每个字符的出现次数,再遍历字符,如果字符出现个数是1,则当前字符对应的小标就是要返回的值 public class Test387 ...
- 力扣 387. 字符串中的第一个唯一字符 C语言实现
题目描述: 给定一个字符串 s ,找到 它的第一个不重复的字符,并返回它的索引 .如果不存在,则返回 -1 . 题目链接 方法一:哈希表 创建一个哈希表,遍历字符串中的字符,并在哈希表中查找该字符,如 ...
- leetcode 387. 字符串中的第一个唯一字符(hash)
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 示例: s = "leetcode" 返回 0 s = "loveleetcode ...
- leetcode 巧妙解法 387. 字符串中的第一个唯一字符
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcod ...
- 2022-1-20 Leetcode 387.字符串中的第一个唯一字符
不能用 set,会导致重复出现的字母第一个无法被更改. class Solution {public:int firstUniqChar(string s) {set<char> mySe ...
最新文章
- java手动输入函数_Java程序如何添加一个函数,如果玩家输入错误,它将返回代码。...
- linux源码安装必备条件,玩转Linux必备的金钥匙之源码安装mysql
- P4201-[NOI2008]设计路线【结论,树形dp】
- Effulgent的《深入理解Direct3D9》整理版(转)
- python成员运算符包括_Python 使用成员运算符案例
- if test 多条件_秒懂Python编程中的if __name__ == #39;main#39; 作用和原理
- Practical Go: Real world advice for writing maintainable Go programs
- 作为游戏策划,软件记录
- 姜启源《数学模型》第12章:马氏链模型
- 网页轮显幻灯片 php,图片轮显代码大全(JS幻灯片)
- Mac SnailSVN不显示状态图标
- 大众-OBD-接口位置
- STM32连接WIFI-ESP8266获取天气信息---STA模式运用
- 大学生HTML5竞赛网站,2019全国大学生信息安全竞赛Web Writeup
- python | 自动化exe程序
- 黑苹果电池电量补丁_小白都能看懂的DSDT电量显示补丁教程
- C++ 机房预约系统
- 处理Oracle数据库服务安全漏洞的几种方法
- PDF快捷工具软件开发(非标)
- html anki编辑器,AnkiEditor | F-Droid - Free and Open Source Android App Repository