当前位置:网站首页>Leetcode advanced path - the first unique character in a string
Leetcode advanced path - the first unique character in a string
2022-06-10 21:18:00 【Li_ XiaoJin】
Given a string , Find its first non repeating character , And return its index . If it doesn't exist , Then return to -1.
Example :
s = "leetcode"
return 0
s = "loveleetcode"
return 2
Tips : You can assume that the string contains only lowercase letters .
public class FirstUniqueCharacterinaString {
public static int firstUniqChar(String s) {
if (s == null || s.length() == 0) {
return -1;
}
if (s.length() == 1) {
return 0;
}
for (int i = 0; i < s.length()-1; i++) {
if (s.indexOf(s.charAt(i)) == s.lastIndexOf(s.charAt(i))) {
return i;
}
}
return -1;
}
/**
* Official solution
* @param s
* @return
*/
public static int firstUniqChar1(String s) {
HashMap<Character, Integer> count = new HashMap<Character, Integer>();
int n = s.length();
for (int i = 0; i < n; i++) {
char c = s.charAt(i);
count.put(c, count.getOrDefault(c, 0) + 1);
}
for (int i = 0; i < n; i++) {
if (count.get(s.charAt(i)) == 1)
return i;
}
return -1;
}
public static void main(String[] args) {
String s = "cc";
// String s = "";
// String s = "abc";
System.out.println(firstUniqChar(s));
}
}
Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/leetcode Advanced road - The first unique character in the string
边栏推荐
- 异步、线程池(CompletableFuture)
- redis设置密码命令(临时密码)
- Diablo immortal wiki address Diablo immortal database address sharing
- Can you still have a wonderful life if you are laid off at the age of 35?
- User defined date component. The left and right buttons control forward or backward year, month, week and day turning
- Node (express) implements interfaces such as adding, deleting, modifying, and paging
- MySQL service startup failed
- ROS virtual time
- Connexion MySQL errorcode 1129, State hy000, Host 'xxx' is Blocked because of many Connection Errors
- 蛮力法/任务分配
猜你喜欢

「运维有小邓」自助帐户解锁工具

A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb

Software definition boundary (SDP)

pdf. Js----- JS parse PDF file to realize preview, and obtain the contents in PDF file (in array form)

CET-6 - Business English - the last recitation before the test

信号与系统复习1

Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"

聊聊服务器性能优化~(建议收藏)

微积分复习1

mysql基础篇之mysql在已有表中添加自动增加的主键(或任意一个字段)
随机推荐
你的公司会选择开发数据中台吗?
Software definition boundary (SDP)
Redis缓存穿透
A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
Quick start to elastic job, three minutes to experience distributed scheduled tasks
Redis cluster configuration
Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?
微积分复习1
app测试用例
Node (express) implements interfaces such as adding, deleting, modifying, and paging
torch. nn. Simple understanding of parameter / / to be continued. Let me understand this paragraph
ros虚拟时间
Meetup Preview: introduction to the new version of linkis and the application practice of DSS
LeetCode:1037. 有效的回旋镖————简单
获取的网络时间 + 时区(+8)
Microsoft Word tutorial, how to change page orientation and add borders to pages in word?
在YUV图像上根据背景色实现OSD反色
异步、线程池(CompletableFuture)
LeetCode 进阶之路 - 125.验证回文串
72. 编辑距离 ●●●