387. First Unique Character in a String
2016-08-22 22:18
465 查看
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.
Subscribe to see which companies asked this question
解题思路:用map存储字符的个数,遍历两遍,确认第一个字符。
Examples:
s = “leetcode”
return 0.
s = “loveleetcode”,
return 2.
Note: You may assume the string contain only lowercase letters.
Subscribe to see which companies asked this question
解题思路:用map存储字符的个数,遍历两遍,确认第一个字符。
public class Solution { public int firstUniqChar(String s) { Map<Character, Integer> map2 = new HashMap<Character,Integer>(); int len = s.length(); for(int i = 0; i < len; i++) { if(map2.containsKey(s.charAt(i))) { int value = map2.get(s.charAt(i)); value ++; map2.put(s.charAt(i),value); }else{ map2.put(s.charAt(i),1); } } for(int i = 0; i < len; i++) { if(map2.get(s.charAt(i))== 1) { return i; } } return -1; } }
相关文章推荐
- Jmeter组件6. SOAP/XML-RPC Request
- 去掉UISegmentedControl的边框颜色
- UE4.11新特性:胶囊体阴影
- urllib.request
- 状态栏(UIStatus)的使用
- 读书笔记-柒-《腾讯网UED体验设计之旅》
- 1.5 Arduino的第一个程序
- CodeForces 653A Bear and Three Balls 【sort+去重(unique)】
- JS生成 UUID的四种方法
- Java中GUI的事件处理
- poj 1679 The Unique MST(次小生成树模版)
- Authentication Error errorcode: 230 uid: -1 appid -1 msg: APP Scode码校验失败
- ural 2071 - Juice Cocktails
- Xcode直接显示图片插件KSImageNamed
- 笔迹宽度估计的低质量文本图像二值化(Robust Document Image Binarization Technique for Degraded Document Images)
- Gradle for Android(二)全局设置、自定义BuildConfig
- Android02--Android之UI的基本控件01
- 修改SystemUI 电池图标当电量为99,直接变为100(工作中用到)
- HDU 5493 Queue (线段树逐个插入)
- CodeForces 446A DZY Loves Sequences