*387. First Unique Character in a String (linkedhashmap + string) debug manunally instead of using leetcode
2024-09-01 12:43:10
The ability to debug and overall thinking need to improve
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Examples:
s = "leetcode"
return 0. s = "loveleetcode",
return 2.
Note: You may assume the string contain only lowercase letters.
Solution: using linkedHashMap<> : have the insertion order!
class Solution {
public int firstUniqChar(String s) {
Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
for(int i = 0; i<s.length(); i++){
Character c = s.charAt(i);
if(map.containsKey(c)){
map.put(c, 2);
}else {
map.put(c,1);
}
}
char temp = '#';
for(Map.Entry<Character, Integer> entry : map.entrySet()){
System.out.println(entry.getKey() + " " + entry.getValue());
if(entry.getValue() == 1){
temp = entry.getKey();
break;
}
}
for(int i = 0; i<s.length(); i++){
if(s.charAt(i) == temp) return i;
}
return -1;
}
}
最新文章
- perl小记
- Studio for WPF:定制 C1WPFChart 标记
- Atitit.电脑图片与拍摄图片的分别
- J2EE 第二阶段项目之编写代码(三)
- javaScript call 函数的用法说明
- Android提升进入界面的速度
- [SDJX2015]面积
- LigerUi中的Grid中不分页显示(local)!
- java中jsoup框架解析html
- C++ 变量的声明与定义的区别
- 不是什么时候都可以用栈来声明对象并使用(自动释放)——Delphi里到处都是编译器魔法,并且自动帮助实例化界面元素指针
- 【LeetCode】25. Reverse Nodes in k-Group
- 【转】如何将ACCESS数据库后缀名accdb改为mdb
- PAT 天梯赛 L2-005 集合相似度
- 【Python】Non-ASCII character '\xe6' 错误解决方法
- 算法精解:最小二乘法C实现
- Redhat 6.3上安装CMake
- IntelliJ IDEA 代码字体大小的快捷键设置放大缩小(很实用)(图文详解)
- .NET MVC项目设置包含Areas中的页面为默认启动页
- 纪中2018暑假培训day1提高b组改题记录