当前位置:网站首页>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
};
边栏推荐
- ESP8266-RTOS物联网开发
- CSP first week of question brushing
- 个人电脑好用必备软件(使用过)
- The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
- 目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
- Visual implementation and inspection of visdom
- LeetCode:387. 字符串中的第一个唯一字符
- 软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
- Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
- R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
猜你喜欢
win10系统中的截图,win+prtSc保存位置
Simple use of promise in uniapp
Cesium draw points, lines, and faces
优秀的软件测试人员,都具备这些能力
Unsupported operation exception
Double pointeur en langage C - - modèle classique
Computer graduation design PHP Zhiduo online learning platform
ESP8266-RTOS物联网开发
Esp8266-rtos IOT development
[embedded] print log using JLINK RTT
随机推荐
C language double pointer -- classic question type
PC easy to use essential software (used)
LeetCode:221. 最大正方形
多元聚类分析
深度剖析C语言数据在内存中的存储
Nacos 的安装与服务的注册
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
Tdengine biweekly selection of community issues | phase III
sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
[sword finger offer] serialized binary tree
To effectively improve the quality of software products, find a third-party software evaluation organization
Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
【嵌入式】使用JLINK RTT打印log
LeetCode:剑指 Offer 42. 连续子数组的最大和
CSP first week of question brushing
win10系统中的截图,win+prtSc保存位置
egg. JS project deployment online server
Computer cleaning, deleted system files
The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
Screenshot in win10 system, win+prtsc save location