zoukankan      html  css  js  c++  java
  • 第一个只出现一次的字符 --剑指offer

    题目描述

    在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
     
     
    思路:建立一个数组 数组的下标是跟字符的ascii码直接相关的 
    public class Solution {
        public int FirstNotRepeatingChar(String str) {
            if(str == null){
                return 0;
            }
            int[] count = new int[58];
            for(int i =0;i < str.length();i ++){
                count[(int)str.charAt(i)-65] += 1;
            }
            for(int i =0;i <str.length(); i ++){
                if(count[(int)str.charAt(i)-65] == 1){
                    return i;
                }
            }
            return -1;
        }
    }
  • 相关阅读:
    uIP的ARP协议分析
    如何使函数不生成执行代码
    计算机网络基础
    [Hive
    [Hive
    [Hive
    [Hive
    [Hive
    [Hive
    [Hive
  • 原文地址:https://www.cnblogs.com/nlw-blog/p/12439814.html
Copyright © 2011-2022 走看看