当前位置:网站首页>LeetCode:387. The first unique character in the string
LeetCode:387. The first unique character in the string
2022-07-06 08:51:00 【Bertil】
Given a string s , find Its first non repeating character , And return its index . If it doesn't exist , Then return to -1 .
Example 1:
Input : s = "leetcode"
Output : 0
Example 2:
Input : s = "loveleetcode"
Output : 2
Example 3:
Input : s = "aabb"
Output : -1
Tips :
- 1 <= s.length <= 10^5
- s Contains only lowercase letters
Their thinking
1. First, use a hash table to count the number of times each letter appears
2. Then iterate through the string , If a character is found to appear only once , Directly return the corresponding subscript , Otherwise, return after the loop ends -1
Code
/** * @param {string} s * @return {number} */
var firstUniqChar = function(s) {
// Create a hash table
let map = new Map()
// Count the times
for (let i = 0; i < s.length; i++) {
let word = s.charAt(i)
if (map.has(word)) {
let val = map.get(word)
map.set(word, val + 1)
} else {
map.set(word, 1)
}
}
// Find the first letter that appears only once
for (let i = 0; i < s.length; i++) {
if (map.get(s.charAt(i)) === 1) {
return i
}
}
return -1
};
边栏推荐
- Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
- LeetCode:498. 对角线遍历
- 数学建模2004B题(输电问题)
- 【嵌入式】Cortex M4F DSP库
- Deep analysis of C language pointer
- Problems encountered in connecting the database of the project and their solutions
- win10系统中的截图,win+prtSc保存位置
- CSP first week of question brushing
- Promise 在uniapp的简单使用
- 同一局域网的手机和电脑相互访问,IIS设置
猜你喜欢

Generator parameters incoming parameters

sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题

LeetCode:124. 二叉树中的最大路径和

Export IEEE document format using latex

MYSQL卸载方法与安装方法

After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF

角色动画(Character Animation)的现状与趋势

Indentation of tabs and spaces when writing programs for sublime text

项目连接数据库遇到的问题及解决

广州推进儿童友好城市建设,将探索学校周边200米设安全区域
随机推荐
[MySQL] multi table query
使用latex导出IEEE文献格式
The harm of game unpacking and the importance of resource encryption
Variable length parameter
Navicat Premium 创建MySql 创建存储过程
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
LeetCode:39. 组合总和
Nacos 的安装与服务的注册
Computer cleaning, deleted system files
Deep anatomy of C language -- C language keywords
LeetCode:劍指 Offer 42. 連續子數組的最大和
Tcp/ip protocol
TP-LINK enterprise router PPTP configuration
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
win10系统中的截图,win+prtSc保存位置
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
Revit 二次开发 HOF 方式调用transaction
JVM quick start
TDengine 社区问题双周精选 | 第三期
vb.net 随窗口改变,缩放控件大小以及保持相对位置