当前位置:网站首页>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
边栏推荐
- pdf.js-----js解析pdf文件实现预览,并获取pdf文件中的内容(数组形式)
- You have to learn math to play art?
- LeetCode 进阶之路 - 167.两数之和 II - 输入有序数组
- 「运维有小邓」自助帐户解锁工具
- A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
- Vissim仿真快速入门
- 蛮力法/任务分配
- 电子招标采购商城系统:优化传统采购业务,提速企业数字化升级
- The excess part of the table setting is hidden. Move the mouse to display all
- Kcon 2022 topic public selection is hot! Don't miss the topic of "favorite"
猜你喜欢

App test case

Construction of RT thread smart win10 64 bit compilation environment

^30h5 web worker multithreading

In MySQL basics, MySQL adds an automatically added primary key (or any field) to an existing table

Understanding deep learning attention

六级考试-商务英语-考前最后一背

LeetCode:497. 非重叠矩形中的随机点————中等

Lengsuanling, a 30-year tortuous history of IPO of a domestic brand

信号与系统复习1

游戏兼容性测试(通用方案)
随机推荐
[generation confrontation network learning part I] classic Gan and its existing problems and related improvements
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?
Stacked bar graph move the mouse into the tooltip to prompt that the filter is 0 element, so as to realize custom bubbles
redis设置密码命令(临时密码)
Redis缓存雪崩
^30h5 web worker multithreading
Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"
leetcode 划分数组使最大差为 K
ROS virtual time
蛮力法/1~n的幂集 v4 递归
连接mysql报错 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors
实用 | 如何利用 Burp Suite 进行密码爆破!
shell实现ssh登录并执行命令
Brute force method /k integers out of 1~n integers
Microsoft Word tutorial, how to change page orientation and add borders to pages in word?
Pytorch deep learning -- neural network convolution layer conv2d
PDF. JS - - - - JS analyse le fichier PDF pour réaliser l'aperçu et obtenir le contenu du fichier PDF (sous forme de tableau)
pdf. Js----- JS parse PDF file to realize preview, and obtain the contents in PDF file (in array form)
Vissim仿真快速入门
^30H5 Web Worker多线程