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

    题目描述

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

    思路

    思路一:

    使用整型数组对出现次数进行统计。

    思路二:

    使用BitSet对出现次数进行统计。 0,1,更多

    代码实现

    package String;
    
    import java.util.BitSet;
    
    /**
     * 第一个只出现一次的字符位置
     * 在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置
     */
    public class Solution51 {
        public static void main(String[] args) {
            Solution51 solution51 = new Solution51();
            String str = "eabbaecdffd";
            System.out.println(solution51.FirstNotRepeatingChar_2(str));
        }
    
        /**
         * 使用BitSet对出现次数进行统计    0,1,更多
         * 对应ASCII码表的256个字符
         *
         * @param str
         * @return
         */
        public int FirstNotRepeatingChar_2(String str) {
            BitSet bs1 = new BitSet(256);
            BitSet bs2 = new BitSet(256);
            for (char c : str.toCharArray()) {
                if (!bs1.get(c) && !bs2.get(c)) {
                    bs1.set(c);     // 0 0
                } else if (bs1.get(c) && !bs2.get(c)) {
                    bs2.set(c); // 0 1
                }
            }
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (bs1.get(c) && !bs2.get(c))
                    return i;
            }
            return -1;
        }
    
        /**
         * 使用整型数组对出现次数进行统计
         * 对应ASCII码表的256个字符
         * 数组的index就是字符, 值为字符出现的次数
         *
         * @param str
         * @return
         */
        public int FirstNotRepeatingChar(String str) {
            int[] cnts = new int[256];
            for (int i = 0; i < str.length(); i++) cnts[str.charAt(i)]++;
            for (int i = 0; i < str.length(); i++) if (cnts[str.charAt(i)] == 1) return i;
            return -1;
        }
    }
    
    
  • 相关阅读:
    实现图片加载时显示百分比思路——serverpush
    我对大项目的看法(定义)
    lucene
    Access常用内置SQL函数
    闰年算法
    DotFuscator使用步骤
    软件加密狗破解思路和方法
    Lucene(.net)学习
    .Net 代码安全保护产品DNGuard HVM使用
    为什么动态创建的控件没有显示出来
  • 原文地址:https://www.cnblogs.com/wupeixuan/p/8878665.html
Copyright © 2011-2022 走看看