leetcode 字符串中的第一个唯一字符
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
案例:
s = "leetcode"
返回 0.
s = "loveleetcode",
返回 2.
注意事项:您可以假定该字符串只包含小写字母。
/**
* @param {string} s
* @return {number}
*/
var firstUniqChar = function (s) {
let len = s.length, obj = {};
for (let i = 0; i !== len; i++) {
if (obj[s[i]] === undefined) {
obj[s[i]] = 0;
} else {
obj[s[i]]++;
}
}
for (let key of Object.keys(obj)) {
if (obj[key] === 0) {
return s.indexOf(key);
}
}
return -1;
};
我又想了好久,明明看上去好像是个比较简单的问题…
然后看一眼大佬的解法,牛皮
/**
* @param {string} s
* @return {number}
*/
var firstUniqChar = function(s) {
for (var i = 0; i < s.length; i++) {
if (s.indexOf(s[i]) == i && s.indexOf(s[i], i + 1) == -1) {
return i;
}
}
return -1;
};
leetcode 字符串中的第一个唯一字符的更多相关文章
- 前端与算法 leetcode 387. 字符串中的第一个唯一字符
目录 # 前端与算法 leetcode 387. 字符串中的第一个唯一字符 题目描述 概要 提示 解析 解法一:双循环 解法二:Set法单循环 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...
- LeetCode初级算法--字符串02:字符串中的第一个唯一字符
LeetCode初级算法--字符串02:字符串中的第一个唯一字符 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog. ...
- 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 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 ...
- LeetCode 387: 字符串中的第一个唯一字符 First Unique Character in a String
题目: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. Given a string, find the first non-repeating charact ...
随机推荐
- halcon连续采集图像
dev_close_window()dev_update_window('off')create_bar_code_model ([], [], BarCodeHandle)dev_open_wind ...
- Java ArrayList的不同排序方法
本文由 ImportNew - 温布利往事 翻译自 dzone.欢迎加入翻译小组.转载请见文末要求. 由于其功能性和灵活性,ArrayList是 Java 集合框架中使用最为普遍的集合类之一.Arra ...
- vector(实现存图)
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #i ...
- 【转】arm和x86的区别
来自: https://blog.csdn.net/u012513972/article/details/78349192/ 信不信,随便逮住一个人问他知不知道CPU,我想他的答案一定会是肯定的,但是 ...
- 自动删除 Elasticsearch 索引
#!/bin/bash # author: Wang XiaoQiang# crontab -e# 0 0 * * * /root/script/del_esindex.sh # auto delet ...
- Spring DI的配置使用
1.1.1 依赖和依赖注入Spring IOC容器的依赖有两层含义:Bean依赖容器和容器注入Bean的依赖资源:a.Bean依赖容器:也就是说Bean要依赖于容器,这里的依赖是指容器负责创建Bean ...
- 动态添加一列到DataTable的第一列
dt.Columns.Add("ROWNUM", typeof(Int64));dt.Columns["ROWNUM"].SetOrdinal(0);
- 转 sql注入
1.判断有无注入点 ; and 1=1 and 1=2 2.猜表一般的表的名称无非是admin adminuser user pass password 等.. and 0<>(selec ...
- solr的客户端操作:使用solrj进行curd操作
导入相关的jar包 <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-s ...
- 61. Rotate List(List)
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...