zoukankan      html  css  js  c++  java
  • 34、第一个只出现一次的字符

    一、题目

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

    二、解法

     1 import java.util.HashMap;
     2 public class Solution {
     3     HashMap<Character,Integer> hashmapValue = new HashMap<Character,Integer>();
     4      public int FirstNotRepeatingChar(String str) {
     5         if(str == null)
     6             return -1;
     7         int len = str.length();//计算字符串长度
     8         //计算每个字符的次数
     9         for(int i = 0; i < len; i++){
    10             if(hashmapValue.containsKey(str.charAt(i))){
    11                 int value = hashmapValue.get(str.charAt(i));
    12                 hashmapValue.put(str.charAt(i), value+1);
    13             }else{
    14                 hashmapValue.put(str.charAt(i), 1);
    15             }
    16         }
    17         for(int i = 0; i < len; i++){
    18             if(hashmapValue.get(str.charAt(i)) == 1)
    19                 return i;
    20         }
    21         return -1;
    22      }
    23 }
  • 相关阅读:
    svn上传文件钩子
    linux服务器版svn安装
    csp-s模拟55
    csp-s模拟54
    csp-s模拟53
    csp-s模拟52
    csp-s模拟51
    csp-s模拟50
    csp-s模拟49
    csp-s模拟48
  • 原文地址:https://www.cnblogs.com/fankongkong/p/7455547.html
Copyright © 2011-2022 走看看