当前位置:网站首页>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
};
边栏推荐
- MongoDB 的安装和基本操作
- 项目连接数据库遇到的问题及解决
- Image, CV2 read the conversion and size resize change of numpy array of pictures
- R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
- Trying to use is on a network resource that is unavailable
- Alibaba cloud server mining virus solution (practiced)
- Detailed explanation of dynamic planning
- Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
- 有效提高软件产品质量,就找第三方软件测评机构
- LeetCode:387. 字符串中的第一个唯一字符
猜你喜欢

生成器参数传入参数

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

MySQL uninstallation and installation methods

UnsupportedOperationException异常

可变长参数

LeetCode:236. 二叉树的最近公共祖先

Mobile phones and computers on the same LAN access each other, IIS settings

使用latex导出IEEE文献格式

LeetCode:221. 最大正方形

Problems in loading and saving pytorch trained models
随机推荐
UnsupportedOperationException异常
Warning in install. packages : package ‘RGtk2’ is not available for this version of R
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
Restful API design specification
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
Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
POI add write excel file
Indentation of tabs and spaces when writing programs for sublime text
LeetCode:162. 寻找峰值
个人电脑好用必备软件(使用过)
Charging interface docking tutorial of enterprise and micro service provider platform
【剑指offer】序列化二叉树
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
How to conduct interface test? What are the precautions? Nanny level interpretation
ROS compilation calls the third-party dynamic library (xxx.so)
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
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
View computer devices in LAN
查看局域网中电脑设备
Navicat Premium 创建MySql 创建存储过程