当前位置:网站首页>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
};
边栏推荐
- The network model established by torch is displayed by torch viz
- Using C language to complete a simple calculator (function pointer array and callback function)
- 自动化测试框架有什么作用?上海专业第三方软件测试公司安利
- Nacos 的安装与服务的注册
- Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
- 如何进行接口测试测?有哪些注意事项?保姆级解读
- TDengine 社区问题双周精选 | 第三期
- What are the common processes of software stress testing? Professional software test reports issued by companies to share
- Roguelike game into crack the hardest hit areas, how to break the bureau?
- Current situation and trend of character animation
猜你喜欢
Double pointeur en langage C - - modèle classique
pytorch训练好的模型在加载和保存过程中的问题
Excellent software testers have these abilities
[sword finger offer] serialized binary tree
Mongodb installation and basic operation
Alibaba cloud server mining virus solution (practiced)
MongoDB 的安装和基本操作
Image,cv2读取图片的numpy数组的转换和尺寸resize变化
Navicat Premium 创建MySql 创建存储过程
TP-LINK 企业路由器 PPTP 配置
随机推荐
Excellent software testers have these abilities
Esp8266-rtos IOT development
Detailed explanation of heap sorting
MYSQL卸载方法与安装方法
Philosophical enlightenment from single point to distributed
TP-LINK enterprise router PPTP configuration
704 binary search
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
[OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)
Bitwise logical operator
View computer devices in LAN
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
超高效!Swagger-Yapi的秘密
LeetCode:387. 字符串中的第一个唯一字符
The network model established by torch is displayed by torch viz
The mysqlbinlog command uses
vb.net 随窗口改变,缩放控件大小以及保持相对位置
自动化测试框架有什么作用?上海专业第三方软件测试公司安利
LeetCode:221. 最大正方形