当前位置:网站首页>[Li Kou 387] the first unique character in the string
[Li Kou 387] the first unique character in the string
2022-07-06 10:37:00 【Spring breeze ~ eleven years】
Title Description : 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 <= 105
- s Contains only lowercase letters
Solution 1 :
We can define one map aggregate ,key Character ,value Is the number of occurrences , Traversal string , Put each character in turn , First count the number of times each character appears , Then traverse the original string to find the first character that appears once , Just return the subscript .
class Solution {
public int firstUniqChar(String s) {
if(s == null) return -1;
HashMap<Character,Integer> map = new HashMap<>();
for(char ch : s.toCharArray()) {
if(map.get(ch) == null) {
map.put(ch,1);
}else{
int count = map.get(ch);
map.put(ch,count+1);
}
}
for(int i = 0;i < s.length();i++) {
if(map.get(s.charAt(i)) == 1) {
return i;
}
}
return -1;
}
}
Solution 2 :
The second time we traverse the above method is the string , We can also traverse the hash map the second time , You only need to traverse all the values in the hash map once , Find out which is not -1 The minimum value of , That is, the index of the first non repeating character . If all values in the hash map are -1, We'll go back to -1, That is, there is no unique character
class Solution {
public int firstUniqChar(String s) {
Map<Character, Integer> position = new HashMap<Character, Integer>();
int n = s.length();
for (int i = 0; i < n; ++i) {
// First traversal string
char ch = s.charAt(i);
if (position.containsKey(ch)) {
position.put(ch, -1);
} else {
// When the string first appears
position.put(ch, i);
}
}
int m = n;
for (Map.Entry<Character, Integer> entry : position.entrySet()) {
// Traverse the hash map for the second time
int pos = entry.getValue();
if (pos != -1 && pos < m) {
m = pos;
}
}
if (m == n) {
// There is no unique character
m = -1;
}
return m;
}
}
边栏推荐
- [leectode 2022.2.13] maximum number of "balloons"
- Mysql25 index creation and design principles
- Database middleware_ MYCAT summary
- Unicode decodeerror: 'UTF-8' codec can't decode byte 0xd0 in position 0 successfully resolved
- MySQL Real Time Optimization Master 04 discute de ce qu'est binlog en mettant à jour le processus d'exécution des déclarations dans le moteur de stockage InnoDB.
- Software test engineer development planning route
- Introduction tutorial of typescript (dark horse programmer of station B)
- 14 medical registration system_ [Alibaba cloud OSS, user authentication and patient]
- 软件测试工程师必备之软技能:结构化思维
- MySQL25-索引的创建与设计原则
猜你喜欢
Nanny hand-in-hand teaches you to write Gobang in C language
Typescript入门教程(B站黑马程序员)
MySQL25-索引的创建与设计原则
保姆级手把手教你用C语言写三子棋
MySQL21-用户与权限管理
Not registered via @enableconfigurationproperties, marked (@configurationproperties use)
MySQL实战优化高手02 为了执行SQL语句,你知道MySQL用了什么样的架构设计吗?
实现微信公众号H5消息推送的超级详细步骤
用于实时端到端文本识别的自适应Bezier曲线网络
In fact, the implementation of current limiting is not complicated
随机推荐
MySQL实战优化高手04 借着更新语句在InnoDB存储引擎中的执行流程,聊聊binlog是什么?
A necessary soft skill for Software Test Engineers: structured thinking
数据库中间件_Mycat总结
MySQL combat optimization expert 09 production experience: how to deploy a monitoring system for a database in a production environment?
在jupyter NoteBook使用Pytorch进行MNIST实现
16 medical registration system_ [order by appointment]
Mysql32 lock
MySQL 29 other database tuning strategies
Pytorch RNN actual combat case_ MNIST handwriting font recognition
MySQL的存储引擎
软件测试工程师发展规划路线
Google login prompt error code 12501
Just remember Balabala
Opencv uses freetype to display Chinese
How to build an interface automation testing framework?
Emotional classification of 1.6 million comments on LSTM based on pytoch
MySQL21-用户与权限管理
Mysql24 index data structure
Not registered via @EnableConfigurationProperties, marked(@ConfigurationProperties的使用)
MySQL25-索引的创建与设计原则