当前位置:网站首页>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
边栏推荐
- synergy: server refused client with our name
- Is Zhongyan futures reliable? Is it a regular futures company? Is it safe to open an account?
- app测试用例
- mysql基础篇
- mysql基础篇之mysql在已有表中添加自动增加的主键(或任意一个字段)
- Nodejs: official document 3 Dgram stream
- Stacked bar graph move the mouse into the tooltip to prompt that the filter is 0 element, so as to realize custom bubbles
- The programmed navigation route jumps to the current route (the parameters remain unchanged), and the navigationduplicated warning error will be thrown if it is executed multiple times?
- 自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日
- LeetCode:497. Random points in non overlapping rectangles -- medium
猜你喜欢

Redis缓存雪崩

View play and earn will lead crypto games astray

Diablo immortal wiki address Diablo immortal database address sharing

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

Nanny tutorial: how to become a contributor to Apache linkis documents
![JS basic and frequently asked interview questions [] = =! [] result is true, [] = = [] result is false detailed explanation](/img/42/bcda46a9297a544b44fea31be3f686.png)
JS basic and frequently asked interview questions [] = =! [] result is true, [] = = [] result is false detailed explanation

App test case

How to use Diablo immortal database

自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日

保姆级教程:如何成为Apache Linkis文档贡献者
随机推荐
Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"
蛮力法/u到v是否存在简单路径
Obtained network time + time zone (+8)
LeetCode 进阶之路 - 125.验证回文串
LeetCode 进阶之路 - 搜索插入位置
View play and earn will lead crypto games astray
Meetup Preview: introduction to the new version of linkis and the application practice of DSS
LeetCode 进阶之路 - 反转字符串
Elastic-Job的快速入门,三分钟带你体验分布式定时任务
72. editing distance ●●
Error code 1129, state HY000, host 'xxx' is blocked because of many connection errors
pytorch深度学习——神经网络卷积层Conv2d
获取的网络时间 + 时区(+8)
Pytorch deep learning -- neural network convolution layer conv2d
蛮力法/1~n个整数中取k个整数
Four methods to obtain the position index of the first n values of the maximum and minimum values in the list
Nodejs: official document 3 Dgram stream
在YUV图像上根据背景色实现OSD反色
Can you still have a wonderful life if you are laid off at the age of 35?
pdf. Js----- JS parse PDF file to realize preview, and obtain the contents in PDF file (in array form)