当前位置:网站首页>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
};
边栏推荐
- 704 binary search
- 自动化测试框架有什么作用?上海专业第三方软件测试公司安利
- Sublime text using ctrl+b to run another program without closing other runs
- Tdengine biweekly selection of community issues | phase III
- Image, CV2 read the conversion and size resize change of numpy array of pictures
- LeetCode:162. 寻找峰值
- Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
- Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
- What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
- Marathon envs project environment configuration (strengthen learning and imitate reference actions)
猜你喜欢

win10系统中的截图,win+prtSc保存位置

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

704 binary search

同一局域网的手机和电脑相互访问,IIS设置

优秀的软件测试人员,都具备这些能力

Trying to use is on a network resource that is unavailable

swagger设置字段required必填

SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date

Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school

Crash problem of Chrome browser
随机推荐
优秀的软件测试人员,都具备这些能力
egg. JS project deployment online server
Indentation of tabs and spaces when writing programs for sublime text
Using C language to complete a simple calculator (function pointer array and callback function)
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
同一局域网的手机和电脑相互访问,IIS设置
The network model established by torch is displayed by torch viz
To effectively improve the quality of software products, find a third-party software evaluation organization
hutool优雅解析URL链接并获取参数
LeetCode:214. 最短回文串
查看局域网中电脑设备
sublime text的编写程序时的Tab和空格缩进问题
Revit 二次开发 HOF 方式调用transaction
Deep analysis of C language data storage in memory
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
Double pointeur en langage C - - modèle classique
Navicat premium create MySQL create stored procedure
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
Unsupported operation exception