剑指Offer------第一个只出现一次的字符
2017-10-05 12:18
302 查看
题目描述
在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置<分析>
采用散列表 --HashMap存储字符和出现的次数
import java.util.HashMap; /** * * @author zy * @date 2017年10月5日 上午11:29:49 * @Decription 在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置 */ public class Ex23 { public int FirstNotRepeatingChar(String str) { HashMap<Character, Integer> hashMap = new HashMap<>(); for (int i = 0; i < str.length(); i++) { if (hashMap.containsKey(str.charAt(i))) { int time = hashMap.get(str.charAt(i)); hashMap.put(str.charAt(i), ++time); }else { hashMap.put(str.charAt(i),1); } } for (int i = 0; i < str.length(); i++) { if (hashMap.get(str.charAt(i))==1) { return i; } } return -1; } }
相关文章推荐
- 剑指offer 第一个只出现一次的字符
- 剑指offer(三十八)之第一个只出现一次的字符位置
- 《剑指Offer》面试题:找到第一个只出现一次的字符
- 《剑指offer》——第一个只出现一次的字符位置
- 剑指Offer(java版):第一个只出现一次的字符
- 剑指offer—第一个只出现一次的字符位置
- 剑指offer:第一个只出现一次的字符位置
- 剑指offer 面试题35 字符串中第一个出现一次的字符
- 剑指OFFER之第一个只出现一次的字符(九度OJ1283)
- 剑指offer(31)-第一个只出现一次的字符位置
- 《剑指offer》刷题笔记(时间空间效率的平衡):第一个只出现一次的字符
- 剑指offer 第一个只出现一次的字符
- 剑指offer----第一个只出现一次的字符
- 剑指Offer——(34)第一个只出现一次的字符位置
- 剑指offer面试题目:第一个只出现一次的字符
- 《剑指offer》-找出字符流中第一个只出现一次的字符
- 字符串中第一个只出现一次的字符——剑指offer面试题40
- JAVA实现 剑指offer—求字符串中第一个只出现一次的字符
- 《剑指offer》——第一个只出现一次的字符
- 【剑指offer系列】 第一个只出现一次的字符___35