问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章

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

    题目: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. Given a string, find the first non-repeating charact ...

  2. [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 ...

  3. 前端与算法 leetcode 387. 字符串中的第一个唯一字符

    目录 # 前端与算法 leetcode 387. 字符串中的第一个唯一字符 题目描述 概要 提示 解析 解法一:双循环 解法二:Set法单循环 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...

  4. Java实现 LeetCode 387 字符串中的第一个唯一字符

    387. 字符串中的第一个唯一字符 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = ...

  5. LeetCode初级算法之字符串:387 字符串中的第一个唯一字符

    字符串中的第一个唯一字符 题目地址:https://leetcode-cn.com/problems/first-unique-character-in-a-string/ 给定一个字符串,找到它的第 ...

  6. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  7. leecode刷题(13) -- 字符串中的第一个唯一字符

    leecode刷题(13) -- 字符串中的第一个唯一字符 字符串中的第一个唯一字符 描述: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = & ...

  8. LeetCode初级算法--字符串02:字符串中的第一个唯一字符

    LeetCode初级算法--字符串02:字符串中的第一个唯一字符 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog. ...

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

    题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...

  10. 字符串中的第一个唯一字符 python

    给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. s = "leetcode" 返回 0. s = "loveleetcode&qu ...

随机推荐

  1. html-webpack-plugin在html中插入数据

    html-webpack-plugin在html中插入数据 <!DOCTYPE html> <html> <head> <meta charset=" ...

  2. Ubuntu18.04安装GitLab搭建私有仓库服务器过程笔记

      百度了很多资料结果折腾很久还没安装成功,索性就直接上官网找文档参考顺利搭建完成 因为有2台服务器做练习,总结了2种安装方式提供参考:第一种官网安装方式,第二种国内镜像安装方式(建议采用第二种) 第 ...

  3. three.js 制作属于自己的动态二维码

    今天郭先生说一下用canvas解析图片流,然后制作一个动态二维码的小案例,话不多说先上图,在线案例点击博客原文.这是郭先生的微信二维码哦! 1. 解析图片流 canvas = document.cre ...

  4. UVALive - 3644 X-Plosives (并查集)

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...

  5. eclipse IDE usage of my own and tutorials link list

    设置 字符集 Eclipse 修改字符集 默认情况下 Eclipse 字符集为 GBK,但现在很多项目采用的是 UTF-8,这是我们就需要设置我们的 Eclipse 开发环境字符集为 UTF-8, 设 ...

  6. OFD呼之欲来、来之跚跚,谁之罪?

    软件国产化的浪潮势不可挡,美国逼得逾甚,我们压迫感逾强,唯有奋起直追方慰平生之志. 在板式文档领域,pdf已成为国际标准,亦可称为美国标准:它在该领域一枝独秀,已形成一览众山小之势! pdf出道20余 ...

  7. Pycharm远程解释器SFTP开发和调试

    转载:https://blog.csdn.net/ll641058431/article/details/53049453 使用PyCharm进行远程开发和调试 你是否经常要在Windows 7或MA ...

  8. linux创建逻辑卷(lv)并挂载

    新加磁盘启动系统后,查看现有磁盘使用情况 命令 df -h 查看现有磁盘情况,我们发现系统已经有一个10G的磁盘sdb,这个磁盘共有1305个柱面,每个柱面大小是8225280 bytes (大约8M ...

  9. org.springframework.beans.factory.UnsatisfiedDependencyException异常

    注解配置不完整 如Service实现类没有加 * @Service * @Transactional

  10. Python os.rmdir() 方法

    概述 os.rmdir() 方法用于删除指定路径的目录.仅当这文件夹是空的才可以, 否则, 抛出OSError.高佣联盟 www.cgewang.com 语法 rmdir()方法语法格式如下: os. ...