当前位置:网站首页>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;
}
边栏推荐
- How slow is the application system on tongweb? How dead is it?
- The reason why the process cannot be shut down after a spark job is executed and the solution
- AUTOSAR实战教程pdf版
- powershell优化之一:提示符美化
- Shardingsphere JDBC < bind table > avoid join Cartesian product
- Mongodb multi field aggregation group by
- Parallelgateway and exclusivegateway of 14 gateways
- Wampserver (MySQL) installation
- About the solution of pychart that cannot be opened by double clicking
- 2021.9.29学习日志-MIME类型
猜你喜欢

16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms

arrayList && linkedList

Vagrant virtual machine installation, disk expansion and LAN access tutorial

How to view tongweb logs correctly?

Django uploads local binaries to the database filefield field

Service architecture diagram of Nacos series

JS output uincode code

Misunderstanding of tongweb due to ease of use

MongoDB 多字段聚合Group by

Mongodb Multi - field Aggregation group by
随机推荐
AUTOSAR实战教程pdf版
Pychart encountered time zone problem when connecting to MySQL timezone
2021.9.29学习日志-Restful架构
Problems encountered in the use of PgSQL
Explanation of service registration and discovery API of Nacos series
Feel the power of shardingsphere JDBC through the demo
A fast week
JS output uincode code
Tongweb customs clearance guidelines
Four shardingsphere JDBC sharding strategies
[China & some provinces and cities] JSON file for offline map visualization
One of PowerShell optimizations: prompt beautification
NVIDIA Jetson Nano/Xavier NX 扩容教程
11 signalthrowingevent and signalboundaryevent of flowable signal event
How to Algorithm Evaluation Methods
Validation set: ‘flowable-executable-process‘ | Problem: ‘flowable-servicetask-missing-implementatio
Timeout thread log for tongweb
MySQL performs an inner join on query. The query result is incorrect because the associated fields have different field types.
Concurrent programming -- source code analysis of thread pool
Byte buddy print execution time and method link tracking