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.

分析:找出字符串中第一次出现且不重复的字符,返回其字符的下标,如果不存在就返回-1.

用hashMap进行求,hashMap中可以通过ey-value的存储位置对和位置,对数据进行标记

  public int firstUniqChar(String s) {
Map<Character,Integer> map=new HashMap<Character,Integer>(); for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(map.containsKey(c)){
map.put(c,-1);
}else{
map.put(c,1);
}
} for(int i=0;i<s.length();i++){
if(map.get(s.charAt(i))==1){
return i;
}
}
return -1;
}

最新文章

  1. 完整部署CentOS7.2+OpenStack+kvm 云平台环境(5)--问题解决
  2. 登录到mysql查看binlog日志
  3. MBR主引导扇区解析
  4. thrift总结
  5. What is a heap?--reference
  6. Java EE的十三种核心技术
  7. Python hashlib模块 (主要记录md5加密)
  8. mac os使用lsusb命令和连接未知的Android设备
  9. JavaScript之再谈回调与闭包
  10. Merge Into 用法
  11. CI表单验证
  12. ubuntu上安装multi-mechanize步骤
  13. 面板JPanel,滚动面板JScrollPane,文本域JTextArea
  14. 词根:lun = moon, 表示“月亮”
  15. 安装jdk1.8导致eclipse显示问题
  16. Failed to load bundle(http://loaclhost:8081/index.bundle?platfrom=ios.....
  17. matlab中hold on 和hold off功能的区别
  18. redis keys 命令
  19. Java基础教程(4)--面向对象概念
  20. MySQL插入记录 insert

热门文章

  1. Spring 4 异常处理
  2. WPF 自定义搜索框
  3. mysql的缓冲查询和非缓冲查询
  4. POJ 2739. Sum of Consecutive Prime Numbers
  5. google protobuf安装与使用
  6. Less:优雅的写CSS代码
  7. 封装的ajax
  8. [LeetCode] Maximum Product of Word Lengths 单词长度的最大积
  9. [LeetCode] Palindrome Linked List 回文链表
  10. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计