Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.

Examples:

s = "leetcode"
return 0. s = "loveleetcode",
return 2.

Note: You may assume the string contain only lowercase letters.

/**
* @param {string} s
* @return {number}
*/ var firstUniqChar = function(s) { if (!s.length) {
return -1;
} let hashed = {};
for (let i = 0; i < s.length; i++) {
const val = s[i];
if (val in hashed) {
hashed[val] = -1;
} else {
hashed[val] = i;
}
} const result = Object.values(hashed).find(e => e >= 0); return result === undefined ? -1: result;
};

[Algorithm] 387. First Unique Character in a String的更多相关文章

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

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

  2. 387. 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. First Unique Character in a String

    Problem: Given a string, find the first non-repeating character in it and return it's index. If it d ...

  4. leetcode修炼之路——387. First Unique Character in a String

    最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...

  5. 18. leetcode 387. 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 ...

  6. [LeetCode&Python] Problem 387. 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 ...

  7. [leetcode]387. 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 ...

  8. Java [Leetcode 387]First Unique Character in a String

    题目描述: Given a string, find the first non-repeating character in it and return it's index. If it does ...

  9. *387. First Unique Character in a String (linkedhashmap + string) debug manunally instead of using leetcode

    The ability to debug and overall thinking need to improve Given a string, find the first non-repeati ...

随机推荐

  1. vs2015 出现的错误lnk:200:-main已在ax.obj中定义

    原因是:一个项目里只能有一个main函数, 如果出现 error:LNK200 的错误,那么需要检查你是不是有两个源代码文件中都定义了main函数. 解决方案:  把其中的一个main函数删掉

  2. 从零开始学C语言

    从零开始学C语言 @阆苑祁寒 更新时间:2019-09-13 写在前面:本文从一个初学者的角度,给出了对C语言的简单理解.如有谬误,敬请指出! Week1——基本语法 #include <std ...

  3. 思维导图xmind的文档保存问题

    如果文件名相同,可能最新的文档覆盖以前的.当前活动文档只能有一个,如果有多个,保存后,其他活动文档也被更新了. 新建一个空白doc文档,仅仅是文件名,作为附件导入到xmind中,在xmind中保存后, ...

  4. oracle grant 授权语句

    --select * from dba_users; 查询数据库中的所有用户 --alter user TEST_SELECT account lock; 锁住用户 --alter user TEST ...

  5. APS.NET MVC + EF (00)---C#基础

    命名参数 命名参数是把参数附上参数名称,这样在调用方法的时候不必按照原来的参数顺序填写参数,只需要对应好参数的名称也能完成方法调用. static void Main(string[] args) { ...

  6. CMPP服务端源码

    CMPP服务端,带数据库,可以接收第三方CMPP客户端的短信,并存入数据库,结合我的cmpp客户端服务程序,将可以实现接收第三方SP的短信并转发到网关实现发送,并将状态报告.上行短信转发给第三方SP, ...

  7. nginx跨域、防盗链、压缩等小功能详解

    原文链接:http://www.studyshare.cn/software/details/1173/0 一.跨域 跨域由来,是因为W3C组织制定的浏览器安全规范,不允许一个域名内的网站在没有别的域 ...

  8. Java自学-面向对象 方法

    Java类的方法 在LOL中,一个英雄可以做很多事情,比如超神,超鬼,坑队友 能做什么在类里面就叫做方法 示例 1 : 什么是方法 比如队友残血正在逃跑,你过去把路给别人挡住了,导致他被杀掉. 这就是 ...

  9. Vue第一天

    什么是 Vue.js? Vue.js是前端的主流框架之一,与 Angular.js.React.js一起,并称为前端三大主流框架 Vue.js是一套构建用户界面的框架,只关注视图层,它不仅易上手,还便 ...

  10. JS this指向总结

    使用 JavaScript 开发的时候,很多开发者多多少少会被 this 的指向搞蒙圈,但是实际上,关于 this 的指向,记住最核心的一句话:哪个对象调用函数,函数里面的this指向哪个对象. 下面 ...