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;
}
}

最新文章

  1. perl小记
  2. Studio for WPF:定制 C1WPFChart 标记
  3. Atitit.电脑图片与拍摄图片的分别
  4. J2EE 第二阶段项目之编写代码(三)
  5. javaScript call 函数的用法说明
  6. Android提升进入界面的速度
  7. [SDJX2015]面积
  8. LigerUi中的Grid中不分页显示(local)!
  9. java中jsoup框架解析html
  10. C++ 变量的声明与定义的区别
  11. 不是什么时候都可以用栈来声明对象并使用(自动释放)——Delphi里到处都是编译器魔法,并且自动帮助实例化界面元素指针
  12. 【LeetCode】25. Reverse Nodes in k-Group
  13. 【转】如何将ACCESS数据库后缀名accdb改为mdb
  14. PAT 天梯赛 L2-005 集合相似度
  15. 【Python】Non-ASCII character '\xe6' 错误解决方法
  16. 算法精解:最小二乘法C实现
  17. Redhat 6.3上安装CMake
  18. IntelliJ IDEA 代码字体大小的快捷键设置放大缩小(很实用)(图文详解)
  19. .NET MVC项目设置包含Areas中的页面为默认启动页
  20. 纪中2018暑假培训day1提高b组改题记录

热门文章

  1. Experimental Educational Round: VolBIT Formulas Blitz A
  2. 微信小程序填坑之旅
  3. Flask&amp;&amp;人工智能AI -- 7 MongoDB
  4. Spring Boot如何初始化数据
  5. TCP并发服务器简单示例
  6. java拦截器的使用
  7. thinkPHP5.0 volist标签理解
  8. set与map
  9. linux在命令符界面如何浏览网页
  10. layui upload封装