当前位置:网站首页>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
};
边栏推荐
- @Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
- Mobile phones and computers on the same LAN access each other, IIS settings
- TP-LINK enterprise router PPTP configuration
- Navicat Premium 创建MySql 创建存储过程
- Revit 二次开发 HOF 方式调用transaction
- Mongodb installation and basic operation
- 项目连接数据库遇到的问题及解决
- 深度剖析C语言指针
- Computer cleaning, deleted system files
- POI add write excel file
猜你喜欢
MongoDB 的安装和基本操作
UML圖記憶技巧
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
LeetCode:498. 对角线遍历
win10系统中的截图,win+prtSc保存位置
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
生成器参数传入参数
Tcp/ip protocol
pytorch训练好的模型在加载和保存过程中的问题
随机推荐
Deep analysis of C language pointer
电脑清理,删除的系统文件
Restful API design specification
生成器参数传入参数
ESP8266-RTOS物联网开发
vb.net 随窗口改变,缩放控件大小以及保持相对位置
Roguelike game into crack the hardest hit areas, how to break the bureau?
JVM quick start
Light of domestic games destroyed by cracking
Deep analysis of C language data storage in memory
使用latex导出IEEE文献格式
Tdengine biweekly selection of community issues | phase III
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
Swagger setting field required is mandatory
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
To effectively improve the quality of software products, find a third-party software evaluation organization
LeetCode:236. 二叉树的最近公共祖先
LeetCode:221. 最大正方形
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
UML圖記憶技巧