当前位置:网站首页>[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;
}
}

边栏推荐
- [after reading the series of must know] one of how to realize app automation without programming (preparation)
- MySQL combat optimization expert 04 uses the execution process of update statements in the InnoDB storage engine to talk about what binlog is?
- Record the first JDBC
- 在jupyter NoteBook使用Pytorch进行MNIST实现
- Mysql25 index creation and design principles
- MySQL 29 other database tuning strategies
- MySQL22-逻辑架构
- Nanny hand-in-hand teaches you to write Gobang in C language
- February 13, 2022 - Maximum subarray and
- Jar runs with error no main manifest attribute
猜你喜欢

【C语言】深度剖析数据存储的底层原理

MySQL35-主从复制

MySQL29-数据库其它调优策略

Just remember Balabala

Mysql25 index creation and design principles

MySQL real battle optimization expert 11 starts with the addition, deletion and modification of data. Review the status of buffer pool in the database

MySQL实战优化高手04 借着更新语句在InnoDB存储引擎中的执行流程,聊聊binlog是什么?

How to build an interface automation testing framework?

Super detailed steps for pushing wechat official account H5 messages

MySQL storage engine
随机推荐
MySQL real battle optimization expert 08 production experience: how to observe the machine performance 360 degrees without dead angle in the process of database pressure test?
Sed text processing
[after reading the series of must know] one of how to realize app automation without programming (preparation)
MySQL21-用户与权限管理
[paper reading notes] - cryptographic analysis of short RSA secret exponents
MySQL实战优化高手05 生产经验:真实生产环境下的数据库机器配置如何规划?
MySQL combat optimization expert 02 in order to execute SQL statements, do you know what kind of architectural design MySQL uses?
① BOKE
MySQL learning diary (II)
Moteur de stockage mysql23
Introduction tutorial of typescript (dark horse programmer of station B)
C language string function summary
MySQL combat optimization expert 06 production experience: how does the production environment database of Internet companies conduct performance testing?
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd0 in position 0成功解决
Global and Chinese markets of static transfer switches (STS) 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of wafer processing robots 2022-2028: Research Report on technology, participants, trends, market size and share
Just remember Balabala
16 medical registration system_ [order by appointment]
用于实时端到端文本识别的自适应Bezier曲线网络
[untitled]