当前位置:网站首页>Leetcode- first unique character in string - simple
Leetcode- first unique character in string - simple
2022-06-13 05:48:00 【AnWenRen】
title :387 The first unique character in the string - Simple
subject
Given a string , Find its first non repeating character , And return its index . If it doesn't exist , Then return to -1.
Example 1
s = "leetcode"
return 0
Example 2
s = "loveleetcode"
return 2
Tips
** Tips :** You can assume that the string contains only lowercase letters .
Code Java
// This method has high time complexity
public int firstUniqChar(String s) {
// 1 use map key Save characters value Save the index
// 2 If there's a repetition Will value Value to length
// 3 Judge map Is it empty If empty Go straight back to -1
// 4 Traverse map Find the smallest value If the last is length return -1
int count = s.length();
HashMap map = new HashMap();
for (int i = 0; i < s.length(); i++) {
if (map.put(s.charAt(i), i) != null) {
map.put(s.charAt(i), count);
}
}
if (map.size() == 0)
return -1;
for (Object value : map.values()) {
int x = (int) value;
if (x < count) {
count = x;
}
}
if (count != s.length())
return count;
else
return -1;
}
// Two for - More slowly
public int firstUniqChar1(String s) {
for (int i = 0; i < s.length(); i++) {
int j;
for (j= 0; j < s.length(); j++) {
if (j == i)
continue;
if (s.charAt(i) == s.charAt(j))
break;
}
if (j == s.length()) {
return i;
}
}
return -1;
}
// An optimization method 1 It's still slow.
public int firstUniqChar2(String s) {
HashMap<Character, Integer> map = new HashMap();
for (int i = 0; i < s.length(); i++) {
if (map.put(s.charAt(i), i) == null) {
map.put(s.charAt(i), 1);
} else {
map.put(s.charAt(i), (int) map.get(s.charAt(i)) + 1);
}
}
for (int i = 0; i < s.length(); ++i) {
if (map.get(s.charAt(i)) == 1) {
return i;
}
}
return -1;
}
边栏推荐
- 12 error end event and terminateendevent of end event
- The reason why the process cannot be shut down after a spark job is executed and the solution
- 2021.9.29 learning log MIME type
- Quartz database storage
- Sentinel series introduction to service flow restriction
- Standard input dialog for pyqt5 qinputdialog
- 若依框架=》如何设置导入导出模板全局为文本格式(解决科学计数问题)
- Building a stand-alone version of Nacos series
- Shardingsphere JDBC exception: no table route info
- 2021.9.29学习日志-Restful架构
猜你喜欢
10 signalstartevent and signalcatchingevent of flowable signal events
Validation set: ‘flowable-executable-process‘ | Problem: ‘flowable-servicetask-missing-implementatio
Solution to prompt "permission is required to perform this operation" (file cannot be deleted) when win10 deletes a file
Information collection for network security (2)
OpenGL mosaic (VIII)
Service fusing and degradation of Note Series
[China & some provinces and cities] JSON file for offline map visualization
为什么那么多人讨厌A-Spice
Wampserver (MySQL) installation
20 flowable container (event sub process, things, sub process, pool and pool)
随机推荐
How slow is the application system on tongweb? How dead is it?
Quartz basic use
MySQL built-in functions
Mysql database crud operation
Service architecture diagram of Nacos series
Database design
Windbos common CMD (DOS) command set
Celery understands
Basic operations of MySQL auto correlation query
Etcd understanding of microservice architecture
Vagrant virtual machine installation, disk expansion and LAN access tutorial
Feel the power of shardingsphere JDBC through the demo
About Evaluation Metrics
16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms
10 signalstartevent and signalcatchingevent of flowable signal events
Function and application scenario of field setaccessible() method
Mysql database backup and restore:
为什么那么多人讨厌A-Spice
2021.9.29 learning log restful architecture
Application virtual directory static resource configuration on tongweb