首页 > 其他 > 详细

34第一个只出现一次的字符

时间:2017-12-31 20:03:51      阅读:233      评论:0      收藏:0      [点我收藏+]

题目描述

在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置

思路:
用数组建一个哈希表,key 是字符,val是次数。
第一次遍历,统计次数
第二次遍历,把次数为1的下标输出。

 1 public class Solution {
 2     public int FirstNotRepeatingChar(String str) {
 3         int[] cnt =  new int[256];
 4         if(str.length()<0) return -1;
 5         for(int i = 0;i<str.length();i++){
 6             cnt[str.charAt(i)-‘A‘]++;
 7         }
 8         for(int i = 0;i<str.length();i++){
 9             if(cnt[str.charAt(i)-‘A‘]==1)
10                 return i;
11         }
12         return -1;
13     }
14 }

 

34第一个只出现一次的字符

原文:https://www.cnblogs.com/zle1992/p/8158146.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!