当前位置:网站首页>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
};
边栏推荐
- Warning in install. packages : package ‘RGtk2’ is not available for this version of R
- 电脑F1-F12用途
- Crash problem of Chrome browser
- 深度剖析C语言指针
- What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
- LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
- sublime text的编写程序时的Tab和空格缩进问题
- [sword finger offer] serialized binary tree
- 项目连接数据库遇到的问题及解决
- Tcp/ip protocol
猜你喜欢
PC easy to use essential software (used)
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
TP-LINK enterprise router PPTP configuration
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
Delay initialization and sealing classes
sublime text的编写程序时的Tab和空格缩进问题
个人电脑好用必备软件(使用过)
The harm of game unpacking and the importance of resource encryption
电脑清理,删除的系统文件
ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法
随机推荐
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born
Cesium draw points, lines, and faces
C语言双指针——经典题型
Simple use of promise in uniapp
Bitwise logical operator
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
Excellent software testers have these abilities
Detailed explanation of heap sorting
Variable length parameter
POI add write excel file
LeetCode:41. 缺失的第一个正数
[OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)
TP-LINK enterprise router PPTP configuration
如何进行接口测试测?有哪些注意事项?保姆级解读
Swagger setting field required is mandatory
The harm of game unpacking and the importance of resource encryption
ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法
How to conduct interface test? What are the precautions? Nanny level interpretation
Navicat Premium 创建MySql 创建存储过程
LeetCode:劍指 Offer 42. 連續子數組的最大和